我可能已经为您找到了解决方法。你试过一个AVAssetExportSession
吗?
在下面的示例中,我构建了一个在屏幕上有两个按钮的简单应用程序。一个调用onSaveBtn:
,它只是抓取我在我的应用程序资源包中拥有的视频的 URL,并将其保存到用户保存的相册中。(不过,就我而言,我的视频确实YES
从. 返回videoAtPathIsCompatibleWithSavedPhotosAlbum:
。我没有任何视频不会返回。)
第二个按钮连接到onExportBtn:
,它获取我们要保存的视频,创建一个AVAssetExportSession
,将视频导出到临时目录,然后将导出的视频复制到保存的相册中。由于导出时间的原因,此方法确实比简单的复制需要更长的时间,但也许这可能是一个替代路径 - 检查结果videoAtPathIsCompatibleWithSavedPhotosAlbum:
,如果是YES
,则直接复制到相册。否则,导出视频,然后复制。
如果没有不返回NO
兼容性调用的视频文件,我不能 100% 确定这对您有用,但值得一试。
您可能还想查看这个问题,该问题探讨了您可能正在使用的设备上兼容的视频格式。
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
- (IBAction)onSaveBtn:(id)sender
{
NSURL *srcURL = [[NSBundle mainBundle] URLForResource:@"WP_20121214_001" withExtension:@"mp4"];
[self saveToCameraRoll:srcURL];
}
- (IBAction)onExportBtn:(id)sender
{
NSURL *srcURL = [[NSBundle mainBundle] URLForResource:@"WP_20121214_001" withExtension:@"mp4"];
AVAsset *srcAsset = [AVAsset assetWithURL:srcURL];
// create an export session
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:srcAsset presetName:AVAssetExportPresetHighestQuality];
// Export the file to a tmp dir
NSString *fileName = [srcURL lastPathComponent];
NSString *tmpDir = NSTemporaryDirectory();
NSURL *tmpURL = [NSURL fileURLWithPath:[tmpDir stringByAppendingPathComponent:fileName]];
exportSession.outputURL = tmpURL;
exportSession.outputFileType = AVFileTypeQuickTimeMovie;
[exportSession exportAsynchronouslyWithCompletionHandler:^{
// now copy the tmp file to the camera roll
switch ([exportSession status]) {
case AVAssetExportSessionStatusFailed:
NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);
break;
case AVAssetExportSessionStatusCancelled:
NSLog(@"Export canceled");
break;
case AVAssetExportSessionStatusCompleted:
NSLog(@"Export successful");
[self saveToCameraRoll:exportSession.outputURL];
break;
default:
break;
}
}];
}
- (void) saveToCameraRoll:(NSURL *)srcURL
{
NSLog(@"srcURL: %@", srcURL);
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock =
^(NSURL *newURL, NSError *error) {
if (error) {
NSLog( @"Error writing image with metadata to Photo Library: %@", error );
} else {
NSLog( @"Wrote image with metadata to Photo Library %@", newURL.absoluteString);
}
};
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:srcURL])
{
[library writeVideoAtPathToSavedPhotosAlbum:srcURL
completionBlock:videoWriteCompletionBlock];
}
}