我从一个NSString开始,它是指向一个.mp4文件的url。我想将该视频保存到设备相机胶卷中。似乎我只能保存.mov文件,因此我必须先转换.mp4文件,但我看到的几篇文章都没有帮助。
有人能帮我完成这个任务吗?
提前感谢...
有人能帮我完成这个任务吗?
提前感谢...
只要使用支持的编解码器,可以将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);
}];
NSURLConnection
或第三方库下载文件。你仍然需要将其写入临时文件,然后调用UISaveVideoAtPathToSavedPhotosAlbum
。 - jonahb