251

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
Satyam
  • 15,493
  • 31
  • 131
  • 244
  • https://stackoverflow.com/questions/4169677/accessing-uiimage-properties-without-loading-in-memory-the-image/4170099#4170099 – black_pearl Nov 06 '19 at 10:39

8 Answers8

478
let heightInPoints = image.size.height
let heightInPixels = heightInPoints * image.scale

let widthInPoints = image.size.width
let widthInPixels = widthInPoints * image.scale
Ryan Brodie
  • 6,554
  • 8
  • 40
  • 57
Jgubman
  • 4,948
  • 1
  • 14
  • 8
  • 6
    Points. Multiply by the scale property to get the pixels. – rmooney Jun 28 '16 at 14:50
  • 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? – Henry Oct 31 '16 at 00:28
  • 1
    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 same – benrudhart Mar 16 '17 at 11:46
  • 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. – Austin Whitelaw Jan 28 '20 at 16:15
37

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

Anomie
  • 92,546
  • 13
  • 126
  • 145
12
UIImage *img = [UIImage imageNamed:@"logo.png"];

CGFloat width = img.size.width;
CGFloat height = img.size.height;
sKhan
  • 9,694
  • 16
  • 55
  • 53
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
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;
Sinuhe Huidobro
  • 191
  • 2
  • 7
5

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
        }
    }
}
Satyam
  • 15,493
  • 31
  • 131
  • 244
Bruno Eigenmann
  • 346
  • 3
  • 16
1
    import func AVFoundation.AVMakeRect

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

    x = imageRect.minX

    y = imageRect.minY
ironRoei
  • 2,049
  • 24
  • 45
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) ;
Sayeed S. Alam
  • 445
  • 2
  • 6
-4
let imageView: UIImageView = //this is your existing imageView

let imageViewHeight: CGFloat = imageView.frame.height

let imageViewWidth: CGFloat = imageView.frame.width
Shamas S
  • 7,507
  • 10
  • 46
  • 58
Wyatt
  • 73
  • 5