Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Get displayed image size in pixels

Tags:

In my app, I'm displaying an image of a rectangle from the assets library. The image is 100x100 pixels. I'm only using the 1x slot for this asset.

I want to display this image at 300x300 pixels. Doing this using points is quite simple but I can't figure out how to get UIImageView to set the size in pixels.

Alternatively, if I can't set the size in pixels to display, I'd like to get the size in pixels that the image is being displayed.

I have tried using .scale on the UIImageView and UIImage instances, but it's always 1. Even though I have set constraints to 150 and 300.

like image 988
Misha M Avatar asked Jun 21 '16 04:06

Misha M


People also ask

How do I check the pixel size of a picture on my iPhone?

Answer: A: Answer: A: Get the Actual Pixels App from the App Store. It's free and will tell you the pixel size of any photo you open.

How do I scale an image in Xcode?

To get the native size of the image just select the image and press Command + = on the keyboard. the to re-size it proportionally select the corner and hold down the shift key when you re-size it.

What are points in iOS?

An iOS point is equivalent to 1/163 of an inch. This size is always the same regardless of the resolution of the phone it is on, and comes from the 163DPI of the original iPhone.


2 Answers

To get size in pixels of UIImageView:

let widthInPixels = imageView.frame.width * UIScreen.mainScreen().scale let heightInPixels = imageView.frame.height * UIScreen.mainScreen().scale 

To get size in pixels of UIImage:

let widthInPixels = image.size.width * image.scale let heightInPixels = image.size.height * image.scale 
like image 95
Code Avatar answered Nov 03 '22 08:11

Code


Swift 5

Take a Look here

// This extension to save ImageView as UImage     extension UIView {         func asImage() -> UIImage {             let renderer = UIGraphicsImageRenderer(bounds: bounds)             return renderer.image { rendererContext in                 layer.render(in: rendererContext.cgContext)             }         }     }      // this extension to resize image      extension UIImage {       func resizeImage(targetSize: CGSize) -> UIImage {         let size = self.size         let widthRatio  = targetSize.width  / size.width         let heightRatio = targetSize.height / size.height         let newSize = widthRatio > heightRatio ?  CGSize(width: size.width * heightRatio, height: size.height * heightRatio) : CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)         let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)          UIGraphicsBeginImageContextWithOptions(newSize, false, 1.0)         self.draw(in: rect)         let newImage = UIGraphicsGetImageFromCurrentImageContext()         UIGraphicsEndImageContext()          return newImage!       }     }      // This extension will display image with 300X300 pixels     extension UIImage {     func Size300X300() -> UIImage? {             let imageView = UIImageView()              imageView.contentMode = .scaleAspectFit              imageView.frame = CGRect(x: 0, y: 0, width: 300, height: 300)              let image = imageView.asImage()              let newImage = image.resizeImage(targetSize: CGSize(width:300, height: 300))              return newImage            }     } 
     let image = YOURIMAGE.Size300X300()      imageView.image = image! 
like image 29
Faris Avatar answered Nov 03 '22 08:11

Faris