CGImageSourceCreateThumbnailAtIndex() returning wrong width or height resolution

I downsample images using the below code. In the case of 300 dpi JPEG images, the obtained a resized image whose metadata contain a DPIWidth of 300 dpi as expected but a DPIHeight of 72 dpi while the resolution should remain unchanged. The resized image therefore looks stretched vertically in a UIImageView.


I encounter this issue with Xcode 15.2 (15C500b) and iOS 17.2.

Does anyone encounter the same issue?


let maxSize = pwgPhotoMaxSizes(rawValue: upload.photoMaxSize)?.pixels ?? Int.max
let resizeOptions = [kCGImageSourceCreateThumbnailFromImageAlways : true,
                     kCGImageSourceCreateThumbnailWithTransform   : true,
                     kCGImageSourceThumbnailMaxPixelSize          : maxSize] as [CFString : Any]

guard let resized = CGImageSourceCreateThumbnailAtIndex(sourceRef, imageIndex,
                                                        resizeOptions as CFDictionary) else {
       // Could not retrieve primary image
       let error = NSError(domain: "…", code: …, userInfo: […)
       failure(error)
       return
}

iPhone 14 Pro, iOS 17

Posted on Feb 11, 2024 2:24 AM

Reply

Similar questions

2 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

CGImageSourceCreateThumbnailAtIndex() returning wrong width or height resolution

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.