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.
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
Specifying One Dimension of Cells in Uicollectionview Using Auto Layout
Is There a Public Way to Force Mpnowplayinginfocenter to Show Podcast Controls
How to Open Maps App Programmatically with Coordinates in Swift
Xcode 4: Build Failed, No Issues
Ios11 Photo Library Access Is Possible Even If Settings Are Set to "Never"
How to Programmatically Add a Proxy to an Nsurlsession
How to Retrieve Keystrokes from a Custom Keyboard on an iOS App
iOS Uiview Subclass, Draw See-Through Text to Background
Https iOS with Self Signed Certificate
How to Pass Data from One Container to Another, Both Embedded in the Same Uiviewcontroller in Swift
Maximum Height of iOS 8 Today Extension
Googlemaps Basic iOS Demo App Crash - Unrecognized Selector Sent to Instance
Implementing Fast and Efficient Core Data Import on iOS 5
Error Itms-90164/90046: Invalid Code Signing Entitlements
Difference Between Presentviewcontroller and Uinavigationcontroller
Startupdatelocations in Background, Didupdatingtolocation Only Called 10-20 Times