kintan
kintan

Reputation: 19

Converting to JPEG given image data and UTI

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  

    }  
}) 

Upvotes: 2

Views: 2691

Answers (2)

Asleepace
Asleepace

Reputation: 3745

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

Upvotes: 3

Vahan Babayan
Vahan Babayan

Reputation: 723

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.

Upvotes: 2

Related Questions