【发布时间】:2014-02-02 23:22:44
【问题描述】:
我从一个 NSString 开始,它是一个 .mp4 文件的 url,从这里我想将该视频保存到设备相机胶卷。看来我只能保存 .mov 文件,所以我必须先转换 .mp4,但我看到的几篇关于此的帖子没有帮助。
谁能帮我完成这个?
提前谢谢...
【问题讨论】:
我从一个 NSString 开始,它是一个 .mp4 文件的 url,从这里我想将该视频保存到设备相机胶卷。看来我只能保存 .mov 文件,所以我必须先转换 .mp4,但我看到的几篇关于此的帖子没有帮助。
谁能帮我完成这个?
提前谢谢...
【问题讨论】:
您可以将 mp4 文件保存到相机胶卷,前提是它使用 supported codecs。例如:
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);
}];
【讨论】:
NSURLConnection 或第三方库下载文件。您仍然需要将其写入临时文件,然后调用 UISaveVideoAtPathToSavedPhotosAlbum。