Nic*_*tto 3 iphone camera objective-c avfoundation ios
即使它看起来像一个简单的程序,现在是3个小时,我正在尝试没有成功.我可能错过了一些非常愚蠢的东西.
所以,我有这个应用程序从互联网上下载视频.视频正确存储在本地,因为我可以播放它们提供本地网址.但是,我无法成功将视频复制到相机胶卷.这是我做的:
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);
}
};
NSLog(@"file %@", localPath);
NSURL *url = [NSURL fileURLWithPath:localPath isDirectory:NO];
[library writeVideoAtPathToSavedPhotosAlbum:url
completionBlock:videoWriteCompletionBlock];
Run Code Online (Sandbox Code Playgroud)
但我得到的输出是:
2013-07-24 00:13:32.094 App[1716:907] file /var/mobile/Applications/70C18C4E-9F97-4A6A-B63E-1BD19961F010/Documents/downloaded_video.mp4
2013-07-24 00:13:32.374 App[1716:907] Wrote image with metadata to Photo Library (null)
Run Code Online (Sandbox Code Playgroud)
当然,文件不会保存在相机胶卷中.它是一个简单的mp4,与我正在使用的设备兼容(即应该可以保存它).
老实说,我不知道该怎么做.任何提示都将受到高度赞赏.谢谢
我可能已经为您找到了解决方法.你试过AVAssetExportSession
吗?
在下面的示例中,我构建了一个简单的应用程序,屏幕上有两个按钮.一个电话onSaveBtn:
,它只是抓取我在应用资源包中的视频的网址,并将其保存到用户保存的相册中.(虽然,在我的情况下,我的视频确实会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];
}
}
Run Code Online (Sandbox Code Playgroud)