2

我有一个 PHPIckerViewController,它从 iOS 14 开始可用。我想从画廊获取图像,它是 WEBP 格式。但是 PHPicker 中的项目提供者无法加载这种格式的图像。请告诉我如何使用新的选择器在 UIButton 上选择和设置图像。

代码:

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

}

谢谢

4

2 回答 2

2

您应该使用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)
          }
        }
      }
    }
  }
}
于 2022-01-01T11:11:23.510 回答
0

经过多次实验,我找到了解决方案。使用“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)
                }
            }
        }
    }
}
于 2021-07-05T13:52:04.157 回答