trdavidson trdavidson - 2 months ago 15
iOS Question

Unable to Save Video to Camera Roll (Objective C)

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!

Answer

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