Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the displayed image frame from UIImageView?

I modify the UIImageView's contentMode property. So my image's frame is not equal to the frame of UIImageView. My problem is how to retrive the image's frame from UIImageView.

As the following example, the orange rectangle is the UIImageView's border. It is obviously that the frame of image is not the same as UIImageView.

Figure 1

like image 718
AechoLiu Avatar asked Jan 17 '11 09:01

AechoLiu


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

How do I change the size of my UIImageView?

The simplest way is to set the frame of your UIImageView and set the contentMode to one of the resizing options. I would NOT retain the result here. newImage will be autoreleased already, and it should up to the method that called this to retain it or not.

How do I change aspect ratio in UIImageView?

To maintain aspect ratio I calculated the width of UIImageView = (320/460)*450 = 313.043 dynamically. And set the contentMode For UIImageView is UIViewContentModeScaleAspectFit. And set the image(320x460) to image view but it is some what blur.


1 Answers

If it's UIViewContentModeScaleAspectFit, it's something like this:

UIImageView *iv; // your image view CGSize imageSize = iv.image.size; CGFloat imageScale = fminf(CGRectGetWidth(iv.bounds)/imageSize.width, CGRectGetHeight(iv.bounds)/imageSize.height); CGSize scaledImageSize = CGSizeMake(imageSize.width*imageScale, imageSize.height*imageScale); CGRect imageFrame = CGRectMake(roundf(0.5f*(CGRectGetWidth(iv.bounds)-scaledImageSize.width)), roundf(0.5f*(CGRectGetHeight(iv.bounds)-scaledImageSize.height)), roundf(scaledImageSize.width), roundf(scaledImageSize.height)); 
like image 85
Costique Avatar answered Sep 28 '22 16:09

Costique