Converting to JPEG given image data and UTI

房东的猫 提交于 2020-01-04 05:47:10

问题


Given Data and its UTI, what's the proper way to convert it to 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  

    }  
}) 

回答1:


Just had this issue with the PHImageManager returning the data as .heic instead .jpeg, this worked for me:

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



回答2:


If it's UTType is JPEG, you already have a proper JPEG encoded representation of the image. And if you want to display that image to your screen, you only need to initialize a UIImage object with the given data calling init?(data:) initializer.



来源:https://stackoverflow.com/questions/44970851/converting-to-jpeg-given-image-data-and-uti

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!