有没有一种方法可以将Live Photo保存到照片库中?

10
我正在传递存储的图像和视频文件到PHLivePhoto.requestLivePhotoWithResourceFileURLs,并获取一个PHLivePhoto对象,可以在PHLivePhotoView中显示。但我想知道,一旦我有了PHLivePhoto,是否有方法将其保存到照片库?

我也在想同样的问题。文档中说:“要将实况照片导入到照片库中,请使用PHAssetCreationRequest类。”但是,creationRequestForAsset方法似乎不接受PHLivePhoto。 https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetCreationRequest_Class/index.html#//apple_ref/occ/clm/PHAssetCreationRequest/creationRequestForAsset - Landon
2个回答

17
    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,但并没有出现。我以为我漏掉了什么东西。我完全忽略了配对视频类型部分... - Landon
2
这个解决方案很好用,但是在我的.mov和.jpg文件中添加元数据之前,我收到了Cocoa错误-1(操作无法完成)的错误。在iOS 11设备上重现了它。 - Stacy Smith
和Stacy Smith一样,在iOS 12上,有人知道为什么吗? - Randall Wang
@StacySmith,你能解释一下你是如何解决这个问题的吗? - Danny

6

Swift 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)”将始终打印一些内容,即使它已经工作了。感谢这个例子。 - Jeff Muir
1
这个解决方案很好,但是在我的.mov和.jpg文件中添加元数据之前,我收到了Cocoa错误-1(操作无法完成)。在iOS 11设备上重现。 - Stacy Smith
似乎在iOS17上无法正常工作。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接