将应用内视频保存到相机胶卷?
Posted
技术标签:
【中文标题】将应用内视频保存到相机胶卷?【英文标题】:Save In-App Video To Camera Roll? 【发布时间】:2012-05-02 03:41:09 【问题描述】:是否可以拍摄嵌入应用程序的视频并将其保存到相机胶卷?如果是这样,怎么做?和 UIWriteImage 一样吗?
【问题讨论】:
@EmilioPelaez 我唯一知道如何写入相机胶卷的是 UIImageWriteToSavedPhotosAlbum,但这仅适用于 UIImages 【参考方案1】:假设您已经知道如何保存图像,您可以尝试此代码:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if([mediaType isEqualToString:(NSString *)kUTTypeMovie])
//Save the video
NSURL *movieUrl = [info objectForKey:UIImagePickerControllerMediaURL];
UISaveVideoAtPathToSavedPhotosAlbum([movieUrl relativePath], self,@selector(movie:didFinishSavingWithError:contextInfo:), nil);
或者你可以试试:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
if([mediaType isEqualToString:(NSString *)kUTTypeMovie])
NSURL *movieUrl = [info objectForKey:UIImagePickerControllerMediaURL];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeVideoAtPathToSavedPhotosAlbum:movieUrl completionBlock:^(NSURL *assetURL, NSError *error)
if(error)
NSLog(@"CameraViewController: Error on saving movie : %@ imagePickerController", error);
else
NSLog(@"URL: %@", assetURL);
];
【讨论】:
【参考方案2】:对于 ios 8 及更高版本,请使用 phphotoLibrary。 ALAssetsLibrary 已弃用。
包括:
@import PhotosUI;
然后用它来保存你的电影:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
[PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:<%Your Movie URL%>];
completionHandler:^(BOOL success, NSError *error)
if (success)
NSLog(@"Movie saved to camera roll.");
else
NSLog(@"Could not save movie to camera roll. Error: %@", error);
];
【讨论】:
Help help help... 我尝试了很多东西。这个答案似乎是最优雅的,但我真的无法让它在我的设备上运行。对于电影 URL,我传递 UIImagePicker 字典值:UIImagePickerControllerMediaURL
,如下所示:file:///private/var/mobile/Containers/Data/Application/A305CE7A-3959-479A-AE61-1BB184C3A679/tmp/83HDFS29__ABAG84H9-AA33-4137-AA2E-BE1JDHFBC41.MOV
我收到此错误:"The operation couldn’t be completed. (Cocoa error -1.)
for the localizedDescription
of Error 对象。以上是关于将应用内视频保存到相机胶卷?的主要内容,如果未能解决你的问题,请参考以下文章