所以我一直试图在相当长的一段时间内完成这项工作,但不幸的是,堆栈上发布的解决方案或我试图写自己的解决方案似乎都没有。我正在构建一个允许用户拍摄照片和视频的应用程序,以及其他用户将其保存下来。我正在使用AWS服务来保存内容。虽然返回的URL使用NSLog,但在将其复制/粘贴到浏览器时向我显示视频,它拒绝保存到相机胶卷。但保存图片效果很好。
到目前为止,我尝试了以下内容:
-(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");
}
有什么建议吗?谢谢!
答案 0 :(得分:7)
*于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];
}
}
}
答案 1 :(得分:1)
目标中-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);
}
}];