无法将视频保存到相机胶卷(目标C) [英] Unable to Save Video to Camera Roll (Objective C)

查看:557
本文介绍了无法将视频保存到相机胶卷(目标C)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经花了相当长的时间尝试完成此操作,但是不幸的是,所有解决方案都没有发布,或者我尝试编写的解决方案似乎都没有用.我正在构建一个应用程序,该应用程序允许用户拍摄照片和视频,而其他用户则可以将其保存下来.我正在使用AWS服务保存内容.尽管使用NSLog返回的URL在将视频复制/粘贴到浏览器时向我显示了视频,但它拒绝保存到相机胶卷中.但是保存图片效果很好.

So I've been trying to accomplish this for quite some time now, but unfortunately none of the solutions posted on stack, or the ones I tried to write myself seem to work. I am building an application that allows users to take pictures and videos, and other users to save these down. I am using AWS services to save the content. Although the returned URL using NSLog, shows me the video when copy/pasting it to a browser, it refuses to save to the camera roll. Saving pictures however works fine.

到目前为止,我尝试了以下操作:

So far I tried the following:

    -(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");
    }

有什么建议吗?谢谢!

推荐答案

* 2016年4月6日更新,以利用现代框架

*Updated on April 6, 2016 to make use of modern frameworks

在您放置此方法的任何位置导入以下内容:

Import the following in wherever you place this method:

#import <AssetsLibrary/AssetsLibrary.h>
@import Photos

然后调用方法如下:

[yourClass saveMedia:(*your image*) video:(*your video url*)]

希望这可以帮助人们随时提出问题.

Hope this helps people, feel free to comment with questions.

+ (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];
    }
   }
}

这篇关于无法将视频保存到相机胶卷(目标C)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆