Unable to Save Video to Camera Roll (Objective C)

自古美人都是妖i 提交于 2019-12-08 04:10:50

问题


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);
        }
     }];
  }
}  

and also:

    if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(self.post.mediaUrl)) {
            UISaveVideoAtPathToSavedPhotosAlbum(self.post.mediaUrl, self, @selector(video:finishedSavingWithError:contextInfo:),@selector(video:finishedSavingWithError:contextInfo:));
    } else {
        NSLog(@"Incompatible File apparently");
    }

Any advice? Thanks!


回答1:


*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

Then call method as following:

[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];
    }
   }
}



回答2:


In Objective - C

Add this in .h/.m file

#import <Photos/Photos.h>

Save Video to Camera Roll:

 [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                        [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:videoUrl];
                    }
                    completionHandler:^(BOOL success, NSError *error) {
                        if (success)
                        {
                            NSLog(@"Video saved");
                        }else{
                            NSLog(@"%@",error.description);
                        }
                    }];


来源:https://stackoverflow.com/questions/24978490/unable-to-save-video-to-camera-roll-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!