10

I'm passing stored image and video files to: PHLivePhoto.requestLivePhotoWithResourceFileURLs and getting a PHLivePhoto object that I can display in PHLivePhotoView. But I am wondering, once I have a PHLivePhoto is there a way to save it to the Photo Library?

Lawrence D'Anna
  • 2,998
  • 2
  • 22
  • 25
keegan3d
  • 10,357
  • 9
  • 53
  • 77
  • I am wondering the exact same thing. The documentation says "To instead import a Live Photo to the Photos library, use the PHAssetCreationRequest class." However, the creationRequestForAsset method doesn't seem to take a PHLivePhoto. https://developer.apple.com/library/prerelease/ios/documentation/Photos/Reference/PHAssetCreationRequest_Class/index.html#//apple_ref/occ/clm/PHAssetCreationRequest/creationRequestForAsset – Landon Oct 02 '15 at 13:20

2 Answers2

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);
        }];
jlw
  • 3,166
  • 1
  • 19
  • 24
  • Awesome! Last night when I was trying to get this to work I got up to addResourceWithType and expected there to be PHAssetResourceTypeLivePhoto. When there wasn't I figured I was missing something. I completely glazed over the pairedvideo type section... – Landon Oct 02 '15 at 17:09
  • 2
    This solution works fine, but I received Cocoa error -1 (The operation couldn’t be completed.) error until metadata was added to my .mov and .jpg files. Reproduced it on iOS 11 devices. – Stacy Smith Sep 04 '18 at 11:55
  • same as Stacy Smith, also on iOS 12, Anyone knows why? – Randall Wang Oct 19 '18 at 08:19
  • @StacySmith can you explain how you fixed the issue? – Danny Sep 06 '20 at 19:51
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")
    }
carlos16196
  • 706
  • 1
  • 7
  • 10
  • It is possible to add resources with Data as well instead of using a file. Also, the "print(error" will always print something, even if it worked. Thanks for this example. – Jeff Muir Jun 06 '17 at 07:53
  • 1
    This solution works fine, but I received Cocoa error -1 (The operation couldn’t be completed.) until metadata was added to my .mov and .jpg files. Reproduced it on iOS 11 devices. – Stacy Smith Sep 04 '18 at 11:55