将mp4视频保存到设备相机胶卷 [英] save mp4 video to device camera roll

查看:359
本文介绍了将mp4视频保存到设备相机胶卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先只是一个NSString,这是一个.mp4文件的网址,从这里我希望将该视频保存到设备相机胶卷。我似乎只能保存.mov文件,所以我必须首先转换.mp4,但是我看到的关于这个的几个帖子没有帮助。

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?

提前致谢...

推荐答案

您可以将mp4文件保存到相机胶卷中,前提是它使用支持的编解码器。例如:

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

或者,在iOS 6及以上版本中:

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

这篇关于将mp4视频保存到设备相机胶卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆