Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resize a UIImageView to fit the underlying image without moving it?

I have a UIImageView whose frame, set before the image is loaded, is always too large for the image, so when I try to round the corners, for example, nothing happens.

How can I resize the frame so it's the same size as the underlying image, while ensuring that the center point of the UIImageView does not change?

like image 500
Steve N Avatar asked Jun 08 '10 18:06

Steve N


2 Answers

If you change the bounds of a UIView the center will not change.

CGRect bounds;

bounds.origin = CGPointZero;
bounds.size = newImage.size;

imageView.bounds = bounds;
imageView.image = newImage;
like image 165
drawnonward Avatar answered Nov 16 '22 05:11

drawnonward


try something along the following lines.. not tested

CGSize imageSize = image.Size;
CGPoint oldCenter = imageView.center;

// now do some calculations to calculate the new frame size
CGRect rect = CGRectMake(0, 0, imageSize.width, imageSize.height);
imageView.frame = rect;
imageView.center = oldCenter;
like image 2
Sujee Maniyam Avatar answered Nov 16 '22 07:11

Sujee Maniyam