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

问题描述 投票:0回答:3

我有一个 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)
                            }
                        }
                    }
           }
 }
ios swift phpickerviewcontroller
3个回答
7
投票

您应该使用

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

2
投票

经过多次实验我找到了解决方案。 使用“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)
                }
            }
        }
    }
}

1
投票

将 PHPickerConfiguration 设置为:

var config = PHPickerConfiguration(photoLibrary: .shared())
config.preferredAssetRepresentationMode = .current <====

在 PHPickerController 中设置此配置:

let picker = PHPickerViewController(configuration: config)

然后在 func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) 委托回调:

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{
                
        }
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.