Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

save mp4 video to device camera roll

Tags:

ios

iphone

ipad

I'm starting with just an NSString that is a url to an .mp4 file, and from here I would like to have that video saved to the device camera roll. It seems I can only save .mov files, so I have to first convert the .mp4, but the few posts I've seen about this didn't help.

Can anyone help me accomplish this?

Thanks in advance...

like image 458
SeanT Avatar asked Dec 15 '22 02:12

SeanT


1 Answers

You can save an mp4 file to the camera roll provided it uses supported codecs. For example:

NSURL *sourceURL = [NSURL URLWithString:@"http://path/to/video.mp4"];

NSURLSessionTask *download = [[NSURLSession sharedSession] downloadTaskWithURL:sourceURL completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
    NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
    NSURL *tempURL = [documentsURL URLByAppendingPathComponent:[sourceURL lastPathComponent]];
    [[NSFileManager defaultManager] moveItemAtURL:location toURL:tempURL error:nil];
    UISaveVideoAtPathToSavedPhotosAlbum(tempURL.path, nil, NULL, NULL);
}];

[download resume];

Or, on iOS 6 and above:

NSURL *sourceURL = [NSURL URLWithString:@"http://path/to/video.mp4"];
NSURLRequest *request = [NSURLRequest requestWithURL:sourceURL];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    NSURL *documentsURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] firstObject];
    NSURL *tempURL = [documentsURL URLByAppendingPathComponent:[sourceURL lastPathComponent]];
    [data writeToURL:tempURL atomically:YES];
    UISaveVideoAtPathToSavedPhotosAlbum(tempURL.path, nil, NULL, NULL);
}];
like image 200
jonahb Avatar answered Dec 25 '22 00:12

jonahb