2015-10-01 70 views
8

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

+0

我想知道同样的事情。文档中提到“要将Live Photo导入到照片库,请使用PHAssetCreationRequest类。”但是,creationRequestForAsset方法似乎不采用PHLivePhoto。 https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetCreationRequest_Class/index.html#//apple_ref/occ/clm/PHAssetCreationRequest/creationRequestForAsset – Landon

回答

13
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); 
     }]; 
+0

太棒了!昨晚,当我试图让这个工作,我起床addResourceWithType,并期望有PHAssetResourceTypeLivePhoto。当我没有想到我错过了什么。我完全查看了pairedvideo类型的部分... – Landon

5

斯威夫特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") 
    } 
+0

可以使用Data来添加资源,而不是使用文件。此外,“打印(错误”将始终打印一些东西,即使它工作。感谢这个例子。 –

相关问题