Is there a way to save a Live Photo to the Photo Library?
Asked Answered
S

2

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?

Switzer answered 1/10, 2015 at 18:0 Comment(1)
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. developer.apple.com/library/prerelease/ios/documentation/Photos/…Cruciform
H
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);
        }];
Highstrung answered 2/10, 2015 at 16:44 Comment(4)
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...Cruciform
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.Raker
same as Stacy Smith, also on iOS 12, Anyone knows why?Nievelt
@StacySmith can you explain how you fixed the issue?Marbles
S
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")
    }
Sargeant answered 3/2, 2017 at 22:10 Comment(3)
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.Gunrunning
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.Raker
Seems not working now on iOS17Bixby

© 2022 - 2024 — McMap. All rights reserved.