简体   繁体   English

如何调整UIImageView的大小以适应底层图像而不移动它?

[英]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. 我有一个UIImageView,其框架在加载图像之前设置,对于图像来说总是太大,所以当我尝试绕角落时,例如,没有任何反应。

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? 我如何调整框架的大小,使其与底层图像的大小相同,同时确保UIImageView的中心点不会改变?

If you change the bounds of a UIView the center will not change. 如果你改变UIView的界限,中心将不会改变。

CGRect bounds;

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

imageView.bounds = bounds;
imageView.image = newImage;

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM