如何调整 UIImageView 的大小以适应底层图像而不移动它? [英] How to resize a UIImageView to fit the underlying image without moving it?

查看:23
本文介绍了如何调整 UIImageView 的大小以适应底层图像而不移动它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UIImageView,它的框架在加载图像之前设置,对于图像来说总是太大,所以当我尝试圆角时,例如,没有任何反应.

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?

推荐答案

如果改变 UIView 的边界,中心不会改变.

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;

这篇关于如何调整 UIImageView 的大小以适应底层图像而不移动它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆