iosswiftphassetphotokit

Get PHAsset/localIdentifier of image saved to photo library


I'm using UIImageWriteToSavedPhotosAlbum to save images to the Photos app. This was the only function I found that lets you do this.

Here's my code:

import UIKit
import Photos

class ViewController: UIViewController {

    /// photo of a cactus
    let photoURL = URL(string: "https://raw.githubusercontent.com/aheze/DeveloperAssets/master/cactus.png")! /// in my app it's actually a local URL, but this also works
    
    func saveToPhotos() {
        DispatchQueue.global().async {
            let data = try? Data(contentsOf: self.photoURL)
            let image = UIImage(data: data!)
            UIImageWriteToSavedPhotosAlbum(image!, self, #selector(self.image(_:didFinishSavingWithError:contextInfo:)), nil)
        }
    }

    /// called when the image saving is complete
    @objc func image(_ image: UIImage, didFinishSavingWithError error: NSError?, contextInfo: UnsafeRawPointer) {
        print("Finished saving image")
        
        /// how to get localIdentifier of the image?
        let asset = PHAsset() /// obviously this does not work...
        let localIdentifier = asset.localIdentifier
    }
}

The image is successfully saved to the Photos app. However, I need to retain a reference to it, preferably its localIdentifier, so I can find it among the user's other photos in the future.

For example, when I call fetchAllPhotos later, I need some way to locate the cactus photo that I had saved.

var allPhotos: PHFetchResult<PHAsset>?

func fetchAllPhotos() {
    let fetchOptions = PHFetchOptions() /// get all of user's photos
    allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
    
    if let photos = allPhotos {
        photos.enumerateObjects { (asset, index, stop) in


            /// I need to find the image!  
            /// So I need to know its localIdentifier back when I saved it.
            if asset.localIdentifier == "cactus" {
                print("photo has been found")
            }
        }
    }
}

Is there any way I can do this? The UIImageWriteToSavedPhotosAlbum completion handler references the UIImage, so maybe I can create a PHAsset from it? Or maybe I can write something to the UIImage's metadata, I'm not sure.


Solution

  • Try this for the saving image logic:

    try PHPhotoLibrary.shared().performChangesAndWait {
        let imgReq = PHAssetChangeRequest.creationRequestForAsset(from: image)
        self.localID = imgReq.placeholderForCreatedAsset.localIdentifier
     }
    

    You can get the local ID as follows:

    photos.enumerateObjects { (asset, index, stop) in
        if asset.localIdentifier == self.localID {
            print("photo was found")
        }
    }