简体   繁体   English

如何使用 PHPicker 从图库中获取 WebP 图像

[英]How to get WebP images from gallery with PHPicker

I have a PHPIckerViewController which is available since iOS 14. And I want to get image from gallery which is format WEBP.我有一个 PHPIckerViewController,它从 iOS 14 开始可用。我想从 WEBP 格式的图库中获取图像。 But item provider in PHPicker can't load image with this format.但是 PHPicker 中的 item provider 无法加载这种格式的图像。 Please tell me how can I pick and set image on UIButton with new picker.请告诉我如何使用新的选择器在 UIButton 上选择和设置图像。

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谢谢

After many experiments I found a solution.经过多次实验,我找到了解决方案。 use "loadDataRepresentation" instead of "loadInPlaceFileRepresentation" so you can get data and build an image.使用“loadDataRepresentation”而不是“loadInPlaceFileRepresentation”,这样您就可以获取数据并构建图像。

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

You should use itemProvider.loadDataRepresentation to load webp image:您应该使用itemProvider.loadDataRepresentation来加载webp图像:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM