【问题标题】:Saving video file to photo gallery using Swift使用 Swift 将视频文件保存到照片库
【发布时间】:2015-07-01 13:00:32
【问题描述】:

我正在处理一个 20 秒的视频。我正面临着将该视频保存在我的照片库中。视频修剪成功后,我需要调用此函数,任何人都可以帮助我在 Swift 中转换以下代码。我已经粘贴了两个版本的代码,如果有人能识别出我的错误吗?

目标 C

-(void)writeVideoToPhotoLibrary:(NSURL*)aURL
{
    NSURL *url = aURL;
    NSData *data = [NSData dataWithContentsOfURL:url];

    // Write it to cache directory
    NSString *path = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"file.mov"];
    [data writeToFile:path atomically:YES];

    NSLog(@"Path:%@",path);

    // After that use this path to save it to PhotoLibrary

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {

        if (error) {
            NSLog(@"%@", error.description);
        }else {
            NSLog(@"Done :)");
        }

    }];


}

斯威夫特

 func SaveVideoToPhotoLibrary(outputFileURL: NSURL)

    {

        assetsLibrary = ALAssetsLibrary()

        let videoURL = outputFileURL as NSURL?

        if let library = assetsLibrary{

            if let url = videoURL{

                library.writeVideoAtPathToSavedPhotosAlbum(url,
                    completionBlock: {(url: NSURL!, error: NSError!) in

                        print(url)

                        if let theError = error{
                            print("Error happened while saving the video")
                            print("The error is = \(theError)")
                        } else {
                            print("no errors happened")
                        }

                })
            } else {
                print("Could not find the video in the app bundle")
            }

        }

            }

【问题讨论】:

    标签: ios swift video trim


    【解决方案1】:
    func saveToCameraRoll(URL: NSURL!) {
                NSLog("srcURL: %@", URL)
                var library: ALAssetsLibrary = ALAssetsLibrary()
                var videoWriteCompletionBlock: ALAssetsLibraryWriteVideoCompletionBlock = {(newURL: NSURL!, error: NSError!) in
                    if (error != nil) {
                NSLog("Error writing image with metadata to Photo Library: %@", error)
            }
                else {
                NSLog("Wrote image with metadata to Photo Library %@", newURL.absoluteString!)
                }
    
                }
                if library.videoAtPathIsCompatibleWithSavedPhotosAlbum(URL) {
                library.writeVideoAtPathToSavedPhotosAlbum(URL, completionBlock: videoWriteCompletionBlock)
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      • 2016-06-16
      • 2021-01-28
      • 1970-01-01
      相关资源
      最近更新 更多