2

Good day.

I'm using PHPicker in order to implement the use of selecting multiple photos and I've encountered an issue when trying to handle the deselecting action.

When I deselect one of the pre-selected photos from the photo library after opening it up for the second time, all of the images suddenly become nil and I get a Could not coerce an item to class UIImage error in canLoadObject(_:) and loadObject(_:). Because of this, it never enters my if loop and the deselection action can never be done.

func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
    let identifiers = results.compactMap(\.assetIdentifier)
    self.parent.imageIdentifierArray = identifiers
        
    // unpack the selected items
    for image in results {
        if image.itemProvider.canLoadObject(ofClass: UIImage.self) {
            image.itemProvider.loadObject(ofClass: UIImage.self) { [weak self] newImage, error in
                if let error = error {
                    print("Can't load image \(error.localizedDescription)") // GETTING THIS ERROR WHEN I OPEN THE PHOTO LIBRARY FOR THE SECOND TIME AND DESELECT A PHOTO
                } else if let image = newImage as? UIImage {
                    // Add new image and pass it back to the main view
                    DispatchQueue.main.async {
                        if let index = self!.parent.selectedPhotos.firstIndex(where: { $0.pngData() == image.pngData() }) {
                            self!.parent.selectedPhotos.remove(at: index)
                        } else {
                            self!.parent.selectedPhotos.append(image)
                        }
                    }
                }
            }
        } else {
            print("Error loading asset")
        }
    }
    // close the modal view
    parent.isPresented = false
}
1
  • no :( i ended up using an external library called YPImagePicker Commented Feb 27, 2024 at 7:55

1 Answer 1

0
for result in results {
        result.itemProvider.loadObject(ofClass: UIImage.self) { [weak self] object,
            error in
            if let image = object as? UIImage {
                let fileName = result.assetIdentifier
                self?.imageArray.append(ImageData.init(image: image, fileName: fileName ?? "Empty Name"))
            } else {
                let fileName = result.assetIdentifier
                //  let identifiers = results.ompactMap(\.assetIdentifier)
                let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [fileName ?? ""], options: nil)
                
                if let image = fetchResult.firstObject?.image {
                    self?.imageArray.append(ImageData.init(image: image, fileName: fileName ?? "Empty Name"))
                }
            }
            if results.count == self?.imageArray.count {
                print("Images: \(self?.imageArray)")
                DispatchQueue.main.async {
                    self?.collectionView.reloadData()
                }
            }
        }
    }

extension PHAsset {

var image : UIImage {
    var thumbnail = UIImage()
    let imageManager = PHCachingImageManager()

// if you want original image use PHImageManagerMaximumSize as targetSize
    imageManager.requestImage(for: self, targetSize: CGSize(width: 100, height: 100), contentMode: .aspectFit, options: nil, resultHandler: { image, _ in
        thumbnail = image!
    })
    return thumbnail
}

}

there is a workaround, you can get image using PHAsset using asset identifier

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.