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

时间:2014-01-14 18:40:45

标签: ios iphone ipad

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

任何人都可以帮我解决这个问题吗?

提前致谢...

1 个答案:

答案 0 :(得分:12)

如果使用supported codecs,您可以将mp4文件保存到相机胶卷。例如:

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及更高版本上:

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