简体   繁体   English

将mp4视频保存到设备相机胶卷

[英]save mp4 video to device camera roll

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. 我开始只是一个NSString,它是.mp4文件的URL,从这里我希望将该视频保存到设备相机胶卷。 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. 我似乎只能保存.mov文件,所以我必须首先转换.mp4,但是我看到的关于这个的几个帖子没有帮助。

Can anyone help me accomplish this? 任何人都可以帮我完成这个吗?

Thanks in advance... 提前致谢...

You can save an mp4 file to the camera roll provided it uses supported codecs . 如果使用支持的编解码器,您可以将mp4文件保存到相机胶卷。 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: 或者,在iOS 6及更高版本上:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM