How can I get the height and width of an uiimage?
Asked Answered
L

8

256

From the URL Image in Mail

I'm adding image to mail view. It will show full image. But I want to calculate, proportionally change the height and width of the image.

How can I get the height and width of UIImage?

Leblanc answered 9/3, 2011 at 17:24 Comment(1)
#4170177Stew
T
482
let heightInPoints = image.size.height
let heightInPixels = heightInPoints * image.scale

let widthInPoints = image.size.width
let widthInPixels = widthInPoints * image.scale
Teleran answered 9/3, 2011 at 17:28 Comment(4)
Points. Multiply by the scale property to get the pixels.Violetteviolin
It was strange. I took a photo by camera and supposed its height > width. But the points of width was 1280 and height 720. What's problem with them?Hess
when loading image from the camera roll (via UIImagePickerController) the scale always seems to be 1.0 but the image.size doesn't image.size is not given in pixels. (it's actually half the pixel size). -> how do I get the proper pixel size - cannot use UIScreen.main.scale cause that's 3 on iPhone 6/7 Plus, but image.size remains the sameBetter
It seems that image.scale is just 1.0 by default, as mentioned in the previous comment. That being said, I'm getting values like (3000, 2002) for a default simulator image, so that must be the actual pixels and not a point value, right? This post is a few years old so maybe image.size has changed to reflect the actual pixel value.Envious
R
37

Use the size property on the UIImage instance. See the documentation for more details.

Rahn answered 9/3, 2011 at 17:26 Comment(0)
C
12
UIImage *img = [UIImage imageNamed:@"logo.png"];

CGFloat width = img.size.width;
CGFloat height = img.size.height;
Cremate answered 11/6, 2015 at 15:7 Comment(0)
N
6

Some of the anwsers above, don't work well when rotating device.

Try:

CGRect imageContent = self.myUIImage.bounds;
CGFloat imageWidth = imageContent.size.width;
CGFloat imageHeight = imageContent.size.height;
Nine answered 31/10, 2012 at 21:52 Comment(0)
D
6

There are a lot of helpful solutions out there, but there is no simplified way with extension. Here is the code to solve the issue with an extension:

extension UIImage {

    var getWidth: CGFloat {
        get {
            let width = self.size.width
            return width
        }
    }

    var getHeight: CGFloat {
        get {
            let height = self.size.height
            return height
        }
    }
}
Dulcy answered 19/4, 2020 at 21:31 Comment(1)
The result can be wrong if scale had been applied.Ultan
B
1
UIImageView *imageView = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MyImage.png"]]autorelease];

NSLog(@"Size of my Image => %f, %f ", [[imageView image] size].width, [[imageView image] size].height) ;
Broadsword answered 15/12, 2011 at 8:9 Comment(0)
G
1
    import func AVFoundation.AVMakeRect

    let imageRect = AVMakeRect(aspectRatio: self.image!.size, insideRect: self.bounds)

    x = imageRect.minX

    y = imageRect.minY
Gehring answered 23/5, 2019 at 11:6 Comment(1)
Its simple to use from UIImage than using another framework.Leblanc
T
-4
let imageView: UIImageView = //this is your existing imageView

let imageViewHeight: CGFloat = imageView.frame.height

let imageViewWidth: CGFloat = imageView.frame.width
Taryn answered 4/3, 2017 at 5:57 Comment(1)
you show how to get imageView's size, it's not the same with image's sizeRegeneracy

© 2022 - 2024 — McMap. All rights reserved.