转换为JPEG给定的图像数据和UTI

时间:2017-07-07 12:31:38

标签: ios jpeg heif

鉴于数据及其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  

    }  
}) 

2 个答案:

答案 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:)对象。