How to resize a UIImageView to fit the underlying

2019-03-26 14:38发布

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?

2条回答
贪生不怕死
2楼-- · 2019-03-26 15:08

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;
查看更多
叼着烟拽天下
3楼-- · 2019-03-26 15:19

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;
查看更多
登录 后发表回答