鉴于数据及其UTI,将其转换为JPEG的正确方法是什么?
PHImageManager.default().requestImageData(for: asset, options: options, resultHandler: { (imageData: Data?, dataUTI: String?, _, _) in
guard let imageData = imageData, let dataUTI = dataUTI else { return }
if !UTTypeConformsTo(dataUTI as CFString, kUTTypeJPEG) {
//TODO: Convert to JPEG
}
})
答案 0 :(得分:3)
这个问题让PHImageManager以.heic而不是.jpeg的形式返回数据,这对我有用:
PHImageManager.default().requestImageData(for: asset, options: PHImageRequestOptions(), resultHandler: { (data, string, orient, info) in
if string?.range(of: ".heic") != nil || string?.range(of: ".heif") != nil {
let rawImage = UIImage(data: data!)
self.uploadData = UIImageJPEGRepresentation(rawImage!, 1.0)
self.uploadName = "public.jpeg"
} else {
self.uploadData = data
self.uploadName = string
}
self.uploadFileToServer()
})
答案 1 :(得分:2)
如果UTType为JPEG
,则您已经对图像进行了正确的JPEG
编码表示。如果要将该图像显示在屏幕上,只需使用给定数据调用UIImage
初始化程序初始化init?(data:)
对象。