无法将视频保存到相机胶卷(目标 C)
Posted
技术标签:
【中文标题】无法将视频保存到相机胶卷(目标 C)【英文标题】:Unable to Save Video to Camera Roll (Objective C) 【发布时间】:2014-07-27 06:29:03 【问题描述】:所以我已经尝试了很长一段时间了,但不幸的是,堆栈上发布的解决方案都没有,或者我自己尝试编写的解决方案似乎都不起作用。我正在构建一个应用程序,允许用户拍摄照片和视频,并让其他用户保存下来。我正在使用 AWS 服务来保存内容。尽管使用 NSLog 返回的 URL 在将视频复制/粘贴到浏览器时向我显示了视频,但它拒绝保存到相机胶卷。但是保存图片效果很好。
到目前为止,我尝试了以下方法:
-(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);
];
还有:
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.post.mediaUrl))
UISaveVideoAtPathToSavedPhotosAlbum(self.post.mediaUrl, self, @selector(video:finishedSavingWithError:contextInfo:),@selector(video:finishedSavingWithError:contextInfo:));
else
NSLog(@"Incompatible File apparently");
有什么建议吗?谢谢!
【问题讨论】:
【参考方案1】:*2016 年 4 月 6 日更新以利用现代框架
在您放置此方法的任何位置导入以下内容:
#import <AssetsLibrary/AssetsLibrary.h>
@import Photos
然后调用方法如下:
[yourClass saveMedia:(*your image*) video:(*your video url*)]
希望这对人们有所帮助,请随时发表评论。
+ (void)saveMedia:(UIImage *)image video:(NSURL *)video_url
if(image)
if(!image)
return;
[[phphotoLibrary sharedPhotoLibrary] performChanges:^
PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
NSLog(@"%@", changeRequest.description);
completionHandler:^(BOOL success, NSError *error)
if (success)
NSLog(@"saved down");
else
NSLog(@"something wrong");
];
else if (video_url)
if([video_url absoluteString].length < 1)
return;
NSLog(@"source will be : %@", video_url.absoluteString);
NSURL *sourceURL = video_url;
if([[NSFileManager defaultManager] fileExistsAtPath:[video_url absoluteString]])
[[[ALAssetsLibrary alloc] init] writeVideoAtPathToSavedPhotosAlbum:video_url completionBlock:^(NSURL *assetURL, NSError *error)
if(assetURL)
NSLog(@"saved down");
else
NSLog(@"something wrong");
];
else
NSURLSessionTask *download = [[NSURLSession sharedSession] downloadTaskWithURL:sourceURL completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error)
if(error)
NSLog(@"error saving: %@", error.localizedDescription);
return;
NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
NSURL *tempURL = [documentsURL URLByAppendingPathComponent:[sourceURL lastPathComponent]];
[[NSFileManager defaultManager] moveItemAtURL:location toURL:tempURL error:nil];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:tempURL];
NSLog(@"%@", changeRequest.description);
completionHandler:^(BOOL success, NSError *error)
if (success)
NSLog(@"saved down");
[[NSFileManager defaultManager] removeItemAtURL:tempURL error:nil];
else
NSLog(@"something wrong %@", error.localizedDescription);
[[NSFileManager defaultManager] removeItemAtURL:tempURL error:nil];
];
];
[download resume];
【讨论】:
crash in this lineNSURL *tempURL = [documentsURL URLByAppendingPathComponent:[sourceURL lastPathComponent]];
... 我的源网址是 -> /private/var/mobile/Containers/Data/Application/1A12E654-19DB-4C2E-913B-93CA71B2F07C/tmp/RUDiVBDqGad6-SCVideo-Merged.mov
最后是 -> RUDiVBDqGad6-SCVideo-Merged.mov
.... 最后是崩溃 -> *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSFileManager moveItemAtURL:toURL:error:]: source URL is nil' *** First throw call stack:
你能帮帮我?
@jose920405 当然可以,我前段时间写了这篇文章,但我会在今天晚些时候回家时发布更新!
谢谢大家,但我找到了其他方式的解决方案,我需要export the session
,然后使用相同的exportSession.outputUrl
,这样它就可以工作了
如何文件保存视频和图像cameraroll目标c【参考方案2】:
在目标中 - C
将此添加到 .h/.m 文件中
#import <Photos/Photos.h>
将视频保存到相机胶卷:
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
[PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoUrl];
completionHandler:^(BOOL success, NSError *error)
if (success)
NSLog(@"Video saved");
else
NSLog(@"%@",error.description);
];
【讨论】:
以上是关于无法将视频保存到相机胶卷(目标 C)的主要内容,如果未能解决你的问题,请参考以下文章