开发者

Saving a Video to the Photo Library - iPhone SDK

Is there any way for me to save a video in the Documents directory to the Photos Library? I have the link of the vi开发者_运维知识库deo in the documents directory, I just don't know how to save it to the Photos app.

Thanks,

Kevin


If you are saving it in a local directory first, then you can save it as..

    ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
    [assetLibrary writeVideoAtPathToSavedPhotosAlbum:url completionBlock:^(NSURL *assetURL, NSError *error){
        if(error) {
           NSLog(@"error while saving to camera roll %@",[error localizedDescription]);        
        } else {
            //For removing the back up copy from the documents directory           
            NSError *removeError = nil;
            [[NSFileManager defaultManager] removeItemAtURL:url error:&removeError];
            NSLog(@"%@",[removeError localizedDescription]);
        }
    }];


Use the UISaveVideoAtPathToSavedPhotosAlbum function.


Try this

You can also use this code to download and save video from internet to Photos.

NSURL *videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"Your Video Url or Path"]];

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{

    NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];

    dispatch_async(dispatch_get_main_queue(), ^{

        // Write it to cache directory
        NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
       [videoData writeToFile:videoPath atomically:YES];

       // After that use this path to save it to PhotoLibrary
       ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
       [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error)
        {
            if (error)
            {
                NSLog("Error");
            }
            else
            {
                NSLog("Success");
            }

        }];
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜