Unable to Save Video to Camera Roll (Objective C)

微笑、不失礼 提交于 2019-12-06 19:40:28

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

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