首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用PHPicker从图库中获取WebP图像

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

Stack Overflow用户
提问于 2021-07-05 20:07:27
回答 1查看 187关注 0票数 1

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

代码:

代码语言:javascript
复制
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)
                        }
                    }
                }
       }

}

谢谢

EN

回答 1

Stack Overflow用户

发布于 2021-07-05 21:52:04

经过多次试验,我找到了一个解决方案。使用"loadDataRepresentation“而不是"loadInPlaceFileRepresentation”,这样您就可以获取数据并构建镜像。

代码语言:javascript
复制
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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68255880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档