skip to Main Content

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


  1. Chosen as BEST ANSWER

    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)
                    }
                }
            }
        }
    }
    

  2. 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)
              }
            }
          }
        }
      }
    }
    
    Login or Signup to reply.
  3. 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{
                    
            }
        })
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search