Reputation: 79
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)
}
}
}
}
}
Upvotes: 3
Views: 1916
Reputation: 1301
Set PHPickerConfiguration to:
var config = PHPickerConfiguration(photoLibrary: .shared())
config.preferredAssetRepresentationMode = .current <====
Set this configuartion in your PHPickerController:
let picker = PHPickerViewController(configuration: config)
and then inside func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) Delegate callback:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
let provider = results[index].itemProvider
provider.loadDataRepresentation(forTypeIdentifier: "public.image", completionHandler: { photoData, error in
if error == nil, let photoData = photoData, let photo = UIImage(data: photoData){
}else{
}
})
}
Upvotes: 1
Reputation: 795
You should use itemProvider.loadDataRepresentation
to load webp
image:
import PhotosUI
class Coordinator: PHPickerViewControllerDelegate {
init(handler: @escaping (UIImage) -> Void) {self.handler = handler}
let handler: (UIImage) -> Void
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
for itemProvider in results.map({$0.itemProvider}) {
if itemProvider.hasItemConformingToTypeIdentifier(UTType.webP.identifier) {
itemProvider.loadDataRepresentation(forTypeIdentifier: UTType.webP.identifier) {data, err in
if let data = data, let img = UIImage.init(data: data) {
self.handler(img)
}
}
} else {
itemProvider.loadObject(ofClass: UIImage.self) {reading, err in
if let img = reading as? UIImage {
self.handler(img)
}
}
}
}
}
}
Upvotes: 7
Reputation: 79
After many experiments I found a solution. use "loadDataRepresentation" instead of "loadInPlaceFileRepresentation" so you can get data and build an image.
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
picker.dismiss(animated: true)
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()) {
results[0].itemProvider.loadDataRepresentation(forTypeIdentifier: representation) { (data, err) in
DispatchQueue.main.async {
let img = UIImage(data: data!)
self.addPhotoButton.setImage(img, for: .normal)
}
}
}
}
}
Upvotes: 2