Search code examples
swiftuiimageviewuiimage

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


When print(uiimage.size) is called it only gives the width and the height of the original image before it was scaled up or down. Is there anyway to get the dimensions of the aspect fitted image?


Solution

  • 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.