How to Get the Height and Width of an Uiimage

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

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

let widthInPoints = image.size.width
let widthInPixels = widthInPoints * image.scale

show small or bigger height, width image in proper way in uiimage in swift

You won't be able to achieve aspect fitting of the image, filling all the whitespace, for a fixed height with a varying width.

One solution that you can have to display the image without cropping the whitespace, without having any whitespace, and keeping the image's aspect ratio intact is to keep the width varying for the UIImageView, and setting the aspect ratio constraint of the image, instead of setting the height constraint. The UIImageView would automatically enlargen and shrink based on the device width.

Add Aspect ratio constraint

Set the aspect ratio value to match your requirement

Don't forget to set the Content mode to Aspect Fit or Aspect Fill.

If you are using a UITableView to display the contents, you can use UITableViewAutomaticDimension to have dynamic heights for the cells, if that is what's limiting your UIImageView's height.

How to get actual height of the image occupied in imageview for aspect fit

for me :

let image: UIImage = UIImage(named: "") 
print(image.size) // 400x400
let scaleFator = UIScreen.main.scale //3
let height = image.size.height / scaleFator ///here the actual height of image.

Get UIImageView's width and height programmatically issue

you can try this

let imgWidth = myImageView.layer.frame.width
let imgHeight = myImageView.layer.frame.height

print(imgWidth)
print(imgHeight)

How do you get the aspect fit size of a uiimage in a uimageview?

Actually, there is a function in AVFoundation that can calculate this for you:

import AVFoundation

let fitRect = AVMakeRect(aspectRatio: image.size, insideRect: imageView.bounds)

now fitRect.size is the size inside the imageView bounds by maintaining the original aspect ratio.

Get dimension or CGRect of UIImage after AspectFit

Actually, when the image is showed, its size(instance of Image) doesn't change unless you call compressing image functions.

The only problem is how it's drawn on ImageView.
When you use aspect fit to imageview, the image is drawn keeping its own rate between width and height based on the larger one of width and height of ImageView.

So, how to calculate the drawn image size is showed above by @Luan Tran.

Thanks

Hope it help

Programmatically change the height and width of a UIImageView Xcode Swift

The accepted answer in Swift 3:

let screenSize: CGRect = UIScreen.main.bounds
image.frame = CGRect(x: 0, y: 0, width: 50, height: screenSize.height * 0.2)


Related Topics



Leave a reply



Submit