Handling WebP Images When Using PHPickerViewController

Be cautious when using PHPickerViewController

Lee Kah Seng
3 min readMay 23, 2023

This article is originally published at swiftsenpai.com

This week, I worked on a feature that required integration with the PHPickerViewController. Everything went well until I noticed that some of the selected images failed to convert into UIImage in the picker(_:didFinishPicking:) delegate method.

Upon investigation, I discovered that the issue arose because some of the images were in WebP format, and the way to handle image results recommended by Apple did not work for WebP images.

Fortunately, resolving the issue is quite straightforward. All we need to do is load the WebP image as data and convert it to a UIImage.

Let me show you.

Why Does It Fail?

In this WWDC video, Apple recommends using the following code to handle images from a PHPickerViewController:

if itemProvider.canLoadObject(ofClass: UIImage.self) {

// Handle UIImage type
itemProvider.loadObject(ofClass: UIImage.self) { image, error in

guard let resultImage = image as? UIImage else {
return
}

// Do something with `resultImage`
}
}

--

--