Quintin B
Quintin B

Reputation: 5881

Save Image to Photo Library and retrieve the URL

I can save an image to my photo library using the following code:

        PHPhotoLibrary.shared().performChanges({
            PHAssetCreationRequest
                .creationRequestForAssetFromImage(atFileURL: outfileURL)
        }) { (saved, err) in
            print("Saved?", saved)
            if (saved) {
                DispatchQueue.main.async {
                    onComplete(outfileURL.absoluteString)                        
                }
            }
        }

But I am trying to load the image I just saved in an Image (SwiftUI) and I want to get the new images fileURL. The outfileURL is a temp file and is not retained. Please note this is a gif - if that has any bearing.

I am trying to use the PHObjectPlaceholder thing but I still don't know how to get the location out:

            var placeHolder: PHObjectPlaceholder? = nil
            PHPhotoLibrary.shared().performChanges({
                let changeRequest = PHAssetCreationRequest.creationRequestForAssetFromImage(atFileURL: outfileURL)
                placeHolder = changeRequest?.placeholderForCreatedAsset
            }) { (saved, err) in
                print("Saved? \(saved) to location \(placeHolder?)") //<--- AAAARGH!!!!
                if (saved) {
                    DispatchQueue.main.async {
                        
                        onComplete(/*????*/)
                        
                    }
                }
            }
   

Upvotes: 0

Views: 1043

Answers (1)

Dmitrii Kononets
Dmitrii Kononets

Reputation: 94

From PHObjectPlaceholder you can use localIdentifier

let fetchOptions = PHFetchOptions()
let fetchResult: PHFetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [placeholder.localIdentifier], options: fetchOptions)
if let asset = fetchResult.firstObject {
    // Here you can get UIImage from PHAsset
} 

So this is possible solution:

    var placeHolder: PHObjectPlaceholder? = nil
    PHPhotoLibrary.shared().performChanges({
        let changeRequest = PHAssetCreationRequest.creationRequestForAssetFromImage(atFileURL: outfileURL)
        placeHolder = changeRequest?.placeholderForCreatedAsset
    }) { (saved, err) in
        if let localIdentifier = placeHolder?.localIdentifier, saved {
            let fetchOptions = PHFetchOptions()
            let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [localIdentifier], options: fetchOptions)
            if let phAsset = fetchResult.firstObject {
                let targetSize = CGSize(width: CGFloat(phAsset.pixelWidth), height: CGFloat(phAsset.pixelHeight))
                let options = PHImageRequestOptions()
                PHCachingImageManager.default().requestImage(for: phAsset, targetSize: targetSize, contentMode: .aspectFill, options: options) { (uiImage, info) in
                    DispatchQueue.main.async {
                        onComplete(uiImage)
                    }
                }
            }
        }
    }

Upvotes: 1

Related Questions