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

泄露秘密 提交于 2019-12-04 19:51:52

问题


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?


回答1:


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;



回答2:


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;


来源:https://stackoverflow.com/questions/3000009/how-to-resize-a-uiimageview-to-fit-the-underlying-image-without-moving-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!