【问题标题】:Is there a way to save a Live Photo to the Photo Library?有没有办法将实时照片保存到照片库?
【发布时间】:2015-12-29 21:16:12
【问题描述】:

我将存储的图像和视频文件传递给: PHLivePhoto.requestLivePhotoWithResourceFileURLs 并获得一个 PHLivePhoto 对象,我可以在 PHLivePhotoView 中显示该对象。但我想知道,一旦我有了PHLivePhoto,有没有办法将它保存到照片库中?

【问题讨论】:

标签: ios cocoa-touch ios9.1 phlivephoto


【解决方案1】:

斯威夫特 3:

PHPhotoLibrary.shared().performChanges({ 

        let request = PHAssetCreationRequest.forAsset()

        request.addResource(with: .photo, fileURL: photoURL, options: nil)
        request.addResource(with: .pairedVideo, fileURL: videoURL, options: nil)

    }) { (success, error) in

        print(success)
        print(error?.localizedDescription ?? "error")
    }

【讨论】:

  • 也可以使用数据添加资源,而不是使用文件。此外,“print(error” 将始终打印一些东西,即使它有效。感谢这个例子。
  • 此解决方案运行良好,但在将元数据添加到我的 .mov 和 .jpg 文件之前,我收到 Cocoa 错误 -1(操作无法完成。)。在 iOS 11 设备上重现它。
【解决方案2】:
    NSURL *photoURL = ...;
    NSURL *videoURL = ...;   

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            PHAssetCreationRequest *request = [PHAssetCreationRequest creationRequestForAsset];


            //These types should be inferred from your files

            //PHAssetResourceCreationOptions *photoOptions = [[PHAssetResourceCreationOptions alloc] init];
            //photoOptions.uniformTypeIdentifier = @"public.jpeg";

            //PHAssetResourceCreationOptions *videoOptions = [[PHAssetResourceCreationOptions alloc] init];
            //videoOptions.uniformTypeIdentifier = @"com.apple.quicktime-movie";

            [request addResourceWithType:PHAssetResourceTypePhoto fileURL:photoURL options:nil /*photoOptions*/];
            [request addResourceWithType:PHAssetResourceTypePairedVideo fileURL:videoURL options:nil /*videoOptions*/];

        } completionHandler:^(BOOL success, NSError * _Nullable error) {
            NSLog(@"success? %d, error: %@",success,error);
        }];

【讨论】:

  • 太棒了!昨晚当我试图让这个工作时,我起床 addResourceWithType 并预计会有 PHAssetResourceTypeLivePhoto。当没有时,我想我错过了一些东西。我完全对pairedvideo类型部分感到震惊......
  • 此解决方案运行良好,但在将元数据添加到我的 .mov 和 .jpg 文件之前,我收到了 Cocoa 错误 -1(无法完成操作。)错误。在 iOS 11 设备上重现它。
  • 和 Stacy Smith 一样,也在 iOS 12 上,有人知道为什么吗?
  • @StacySmith 你能解释一下你是如何解决这个问题的吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 2017-07-14
  • 2016-03-13
  • 2014-12-14
  • 2013-05-31
相关资源
最近更新 更多