iOS: [NSBundle mainBundle] pathForResource返回路径为nil

以下这段代码在执行时,创建的playerController总是为nil,查找原因:原来是通过[NSBundle mainBundle] 获取的路径为nil,则创建出来的playerController对象时也为nil。

#pragma mark - Lazy Loading
- (IJKFFMoviePlayerController *)playerController {
    if (_playerController == nil) {
        // 随机播放一个视频
        NSString *movieName = arc4random_uniform(10) % 2 ? @"login_video" : @"loginmovie";
        NSString *path = [[NSBundle mainBundle] pathForResource:movieName ofType:@"mp4"];
        IJKFFMoviePlayerController *playerController = [[IJKFFMoviePlayerController alloc]
                                                        initWithContentURLString:path withOptions:[IJKFFOptions optionsByDefault]];
        [self.view addSubview:playerController.view];
        [playerController.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.view);
        }];
        // 填充模式
        playerController.scalingMode = IJKMPMovieScalingModeAspectFill;
        // 自动播放
        playerController.shouldAutoplay = NO;
        // 准备播放
        [playerController prepareToPlay];
        
        _playerController = playerController;
    }
    return _playerController;
}

解决方法: 在Targets -> Build Phases -> Copy Bundle Resources里面导入资源文件,运行项目

iOS: [NSBundle mainBundle] pathForResource返回路径为nil_第1张图片
Snip20161116_17.png

你可能感兴趣的:(iOS: [NSBundle mainBundle] pathForResource返回路径为nil)