Looking at Apple's PHPickerViewController sample code, there is an example of getting a UIImage, but there is no example of getting Data. ..
Of course, converting a UIImage to Data is subtle.
It may be good to give up a hundred steps and use HEIC, JPG or PNG, but the method to convert UIImage to Data is "[pngData ()](https://developer.apple.com/documentation/uikit/uiimage/1624096". There are only two, "-png data)" and "jpegData (compressionQuality :)". If you convert the GIF with these methods, the animation will stop.
After all, I want to get the Data type directly. For such a person, I will write ** How to get Data from PHPickerViewController **.
You can get it by using the NSItemProvider loadDataRepresentation (forTypeIdentifier: completionHandler :)
.
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
results.forEach { result in
result.itemProvider.loadDataRepresentation(forTypeIdentifier: "public.image", completionHandler: { [weak self] data, _ in
guard let data = data else { return }
print(data)
})
}
}
The trick is to specify public.image
for the forTypeIdentifier (Uniform Type Identifier).
By doing this, you can get Data such as HEIC, PNG, JPG, GIF.
If you want to specify the data to be acquired in more detail, use Apple's Uniform Type Identifier Concepts. Please specify for reference.
Recommended Posts