4

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.

aheze
  • 24,434
  • 8
  • 68
  • 125
  • 1
    Refactor your technique to use PHAssetChangeRequest, there is lots of examples online, and the placeholderForCreatedAsset will have the correct localIdentifier. – Pranav Kasetti Jan 04 '21 at 18:31
  • @PranavKasetti that looks very promising. Thanks! – aheze Jan 04 '21 at 18:34

1 Answers1

3

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")
    }
}
Pranav Kasetti
  • 8,770
  • 2
  • 50
  • 71