How to get the size of a scaled UIImage in UIImageView?

The image.size attribute of UIImageView gives the size of the original UIImage. I would like to find out the size of the autoscaled image when it is put in the UIImageView (typically smaller than the original).

For example, I have the image set to Aspect Fit. Now I want to know its new height and width on the screen so I can draw accurately on the new scaled image.

  • How to compare two UIImage in iOS 8
  • iPhone SDK: UIScrollView does not scroll
  • Loading an image into UIImage asynchronously
  • Taking time to load the image from URL to UIImageview
  • Changing the UIImage of a specific UITableViewCell when an event occurs
  • How to make UIImageView automatically resize to the size of the image loaded
  • Is there any way to do this without figuring it out myself based on the UIImageView size & UIImage original size (basically reverse engineering its scaling)?

    7 Solutions Collect From Internet About “How to get the size of a scaled UIImage in UIImageView?”

    -(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView
        float imageRatio = image.size.width / image.size.height;
        float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
        if(imageRatio < viewRatio)
            float scale = imageView.frame.size.height / image.size.height;
            float width = scale * image.size.width;
            float topLeftX = (imageView.frame.size.width - width) * 0.5;
            return CGRectMake(topLeftX, 0, width, imageView.frame.size.height);
            float scale = imageView.frame.size.width / image.size.width;
            float height = scale * image.size.height;
            float topLeftY = (imageView.frame.size.height - height) * 0.5;
            return CGRectMake(0, topLeftY, imageView.frame.size.width, height);

    This simple function will calculate size of image after aspect fit:

        float newwidth;
        float newheight;
        UIImage *image=imgview.image;
        if (image.size.height>=image.size.width){
                float diff=imgview.frame.size.width-newwidth;
                float diff=imgview.frame.size.height-newheight;
        NSLog(@"image after aspect fit: width=%f height=%f",newwidth,newheight);
        //adapt UIImageView size to image size
        return CGSizeMake(newwidth, newheight);

    This simple function will calculate size of image:-

    [imageView setFrame:AVMakeRectWithAspectRatioInsideRect(image.size, imageView.frame)];

    For more details, You can Refer AV Foundation Functions Reference.

    An adaptation of cncool answer, in swift as a UIImageView extension, perhaps it could be useful to someone:

    extension UIImageView{
        func frameForImageInImageViewAspectFit() -> CGRect
            if  let img = self.image {
                let imageRatio = img.size.width / img.size.height;
                let viewRatio = self.frame.size.width / self.frame.size.height;
                if(imageRatio < viewRatio)
                    let scale = self.frame.size.height / img.size.height;
                    let width = scale * img.size.width;
                    let topLeftX = (self.frame.size.width - width) * 0.5;
                    return CGRectMake(topLeftX, 0, width, self.frame.size.height);
                    let scale = self.frame.size.width / img.size.width;
                    let height = scale * img.size.height;
                    let topLeftY = (self.frame.size.height - height) * 0.5;
                    return CGRectMake(0, topLeftY, self.frame.size.width, height);
            return CGRectMake(0, 0, 0, 0)

    Since you’ve got the image view set to Aspect Fit, you can simply take the height and width of the image view along with the height and width of the original image and calculate the height and width of the scaled image.

    That said, the better way of doing this is to have a custom view instead of a UIImageView. Then, draw the image yourself in the custom view. That way you can control every aspect of it.

    How about just get the UIImageView size from its frame? i.e. imageView.frame?

    If the frame is filled maintaining the image aspect ratio, the imageview frame will not be the same as the image size.