【问题标题】:How to save video clip to Photos gallery - ios 8 & 9如何将视频剪辑保存到照片库 - ios 8 和 9
【发布时间】:2016-03-11 08:04:01
【问题描述】:

我已经努力将视频剪辑保存到照片库。 这里的代码如下,但它不起作用。 虽然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)
    }
}

【问题讨论】:

    标签: ios save ios9 image-gallery


    【解决方案1】:
    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
                }
        }
    

    【讨论】:

      【解决方案2】:

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

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

      在这个库中,您必须将 PHAsset 对象提供到用于类别操作的块中。

      【讨论】:

      • 通过桥头文件转换这个库。
      【解决方案3】:

      试试这个:-

      对于目标 - 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 中使用上述代码。

      【讨论】:

      • ALAssetsLibrary 将在 iOS 9.0 中弃用
      • 好的,我会将其转换为 swift 和 iOS 9,然后更新我的答案。好吗?
      • 您可以使用PHPhotoLibrary 代替ALAssetsLibrary
      • 是的,我通过使用 PHPhotoLibrary 获得了解决方案。谢谢
      • @user831098 你能不能给我那个代码或者用那个代码编辑我的答案。
      猜你喜欢
      • 1970-01-01
      • 2014-12-14
      • 1970-01-01
      • 2014-02-02
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2021-01-28
      • 2018-02-04
      相关资源
      最近更新 更多