I have a PHPIckerViewController which is available since iOS 14. And I want to get image from gallery which is format WEBP. But item provider in PHPicker can’t load image with this format. Please tell me how can I pick and set image on UIButton with new picker.
code:
extension SixStepRegistrationViewController: PHPickerViewControllerDelegate {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
let supportedRepresentations = [UTType.rawImage.identifier,
UTType.tiff.identifier,
UTType.bmp.identifier,
UTType.png.identifier,
UTType.jpeg.identifier,
UTType.webP.identifier,
]
for representation in supportedRepresentations {
if results[0].itemProvider.hasRepresentationConforming(toTypeIdentifier: representation, fileOptions: .init()) {
print(representation, " repr")
results[0].itemProvider.loadInPlaceFileRepresentation(forTypeIdentifier: representation) { (originalUrl, inPlace, error) in
DispatchQueue.main.async {
print(originalUrl, " ", inPlace)
self.addPhotoButton.setImage(UIImage(contentsOfFile: originalUrl!.path), for: .normal)
//self.dismiss(animated: true, completion: nil)
}
}
}
}
}
Thanks
3
Answers
After many experiments I found a solution. use "loadDataRepresentation" instead of "loadInPlaceFileRepresentation" so you can get data and build an image.
You should use
itemProvider.loadDataRepresentation
to loadwebp
image:Set PHPickerConfiguration to:
Set this configuartion in your PHPickerController:
and then inside func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) Delegate callback: