【问题标题】:save mp4 video to device camera roll将 mp4 视频保存到设备相机胶卷
【发布时间】:2014-02-02 23:22:44
【问题描述】:

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

谁能帮我完成这个?

提前谢谢...

【问题讨论】:

    标签: ios iphone ipad


    【解决方案1】:

    您可以将 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);
    }];
    

    【讨论】:

    • 非常感谢您的意见,您的解决方案似乎在 iOS 7 中运行良好,但我需要使其在 iOS 6.1+ 中运行。有什么想法吗?
    • 您必须使用NSURLConnection 或第三方库下载文件。您仍然需要将其写入临时文件,然后调用 UISaveVideoAtPathToSavedPhotosAlbum
    • @SeanT,我添加了一个 iOS 6 示例。
    • @jonahb 您能否就我的SO Question 处理大量数据提供一些建议。我有兴趣确保在后台线程上正确处理将数据保存到视频文件。看起来您的解决方案可能有效,但线程的含义并不明显。提前感谢您的想法。
    猜你喜欢
    • 2015-01-06
    • 2013-03-21
    • 2013-07-22
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-12
    • 2015-09-25
    • 2015-06-11
    相关资源
    最近更新 更多