Reputation: 12549
Can I get the pixel size for a UIImage if I just multiply the image.size with the image.scale ? Is it the same as doing this:
guard let cgImage = image.CGImage else{
return nil
}
let width = CGImageGetWidth(cgImage)
let height = CGImageGetHeight(cgImage)
Upvotes: 10
Views: 7922
Reputation: 529
extension UIImage {
func imageSizeInPixel() -> CGSize {
let heightInPoints = size.height
let heightInPixels = heightInPoints * scale
let widthInPoints = size.width
let widthInPixels = widthInPoints * scale
return CGSize(width: widthInPixels, height: heightInPixels)
}
}
Upvotes: 1
Reputation: 689
You can, but it isn't the same as cgImage.width x cgImage.height (the new names for the CG functions you gave). The cgImage doesn't know about at least some EXIF flags that might have rotated or flipped the original image. You can see a difference in image Portrait_5.jpg through Portrait_8.jpg at https://github.com/recurser/exif-orientation-examples. image.size.height * image.scale
gives 1800, but cgImage.height
gives 1200 (really the width).
Upvotes: 6
Reputation: 318794
Yes.
CGImageGetWidth
and CGImageGetHeight
will return the same size (in pixels) as returned by image.size * image.scale
.
Upvotes: 14