问题
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