我有一个从iOS 14开始可用的PHPIckerViewController。我想从画廊得到图像,这是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)
}
}
}
}}
谢谢
发布于 2021-07-05 21:52:04
经过多次试验,我找到了一个解决方案。使用"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)
}
}
}
}
}https://stackoverflow.com/questions/68255880
复制相似问题