我正在使用 imagePickerController 来录制视频。在imagePickerController:didFinishPickingMediaWithInfo:
函数中,我试图将视频设置为先前定义的 MPMoviePlayerController:
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
NSURL *movieUrl = [info objectForKey:UIImagePickerControllerMediaURL];
[self.moviePlayer setContentURL:movieUrl]];
}
这工作正常,视频确实在播放。但我想保存文件以备后用。当我这样做并将保存的文件用于电影播放器时,没有任何反应:
我已经尝试过了(将数据保存到新文件)
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *newPath = [directory stringByAppendingPathComponent:@"myMovie.mov"]; NSData *videoData = [NSData dataWithContentsOfURL:movieUrl]; [videoData writeToFile:newPath atomically:NO]; [self.moviePlayer setContentURL:[NSURL URLWithString:newPath]];
或者这个(将临时视频文件复制到我的文件夹)
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *newPath = [directory stringByAppendingPathComponent:@"myMovie.mov"]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error = nil; [fileManager copyItemAtPath:[videoUrl path] toPath:newPath error:&error]; [self.moviePlayer setContentURL:[NSURL URLWithString:newPath]];
没有任何成功,即使文件newPath
确实存在......我做错了什么?