如何将视频剪辑保存到照片库 - ios 8& 9

时间:2016-03-11 08:04:01

标签: ios save ios9 image-gallery

我已经解决了将视频片段保存到照片库的问题。 这里的代码如下,但它不起作用。 即使saveVideo返回true,但我打开照片库无法获取视频片段。

  

文件路径

是本地文档目录中的位置

func saveVideo(filePath: String) {
  UISaveVideoAtPathToSavedPhotosAlbum (filePath, self,"video:didFinishSavingWithError:contextInfo:", nil)
}

func video(filePath: String, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
    if error == nil {
        let ac = UIAlertController(title: "Saved!", message: "Your altered video has been saved to your gallery.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        self.presentViewController(ac, animated: true, completion: nil)
    } else {
        let ac = UIAlertController(title: "Save error", message: error?.localizedDescription, preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        self.presentViewController(ac, animated: true, completion: nil)
    }
}

3 个答案:

答案 0 :(得分:2)

PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in

        let createAssetRequest: PHAssetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(string: filePath)!)!
        createAssetRequest.placeholderForCreatedAsset

        }) { (success, error) -> Void in
            if success {

                //popup alert success
            }
            else {
               //popup alert unsuccess
            }
    }

答案 1 :(得分:1)

试试这个: -

目标 - C。

此代码用于从互联网下载视频并将其保存到照片库。

NSURL *videoUrl = [NSURL URLWithString:[NSString stringWithFormat:@"Your Video Url"]];

dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(q, ^{

NSData *videoData = [NSData dataWithContentsOfURL:videoUrl];

dispatch_async(dispatch_get_main_queue(), ^{

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

    // After that use this path to save it to PhotoLibrary
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error)
     {
         if (error)
         {
            NSLog("Error");
         }
         else
         {
            NSLog("Success");
         }

        }];
    });
});

在为iOS 9.0或更高版本开发应用时,您可以使用PHPhotoLibrary代替ALAssetsLibrary

您可以通过转换为swift语法在Swift中使用上面的代码。

答案 2 :(得分:1)

尝试此类别来存储和更新PHAsset(图像/视频),

https://github.com/zakkhoyt/PHAsset-Utility

在此库中,您必须将PHAsset对象提供到块中以进行类别操作。