iOS - 是否应该更改 UIImageView 的框架来调整其图像大小?
有人能明确回答这个问题吗?似乎有时更改 UIImageView 的框架会调整其图像大小,有时则不会。
我已经加载了一个图像并用它创建了一个 UIImageView,大小约为 100x100。执行以下操作绝对没有效果:
myImageView.contentMode = UIViewContentModeScaleAspectFit;
myImageView.frame = CGRectMake(0, 0, 30, 30);
显然我想缩小图像,但它只是保持完整尺寸。
编辑:如果我这样做:
myImageView.clipsToBounds = YES;
我会得到图像的 30x30 部分,但只有它的上角。如何将整个图像缩小到 30x30?
Can someone answer this definitively? It seems that sometimes changing the frame of a UIImageView resizes its image and sometimes it doesn't.
I have loaded an image and created a UIImageView with it, sized about 100x100. Doing the following has absolutely no effect:
myImageView.contentMode = UIViewContentModeScaleAspectFit;
myImageView.frame = CGRectMake(0, 0, 30, 30);
Obviously I want to shrink the image down, but it just remains full size.
EDIT: If I do this:
myImageView.clipsToBounds = YES;
I get a clipped 30x30 portion of the image, but only the upper corner of it. How do I shrink the whole image down to 30x30?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
老问题,但就我而言,问题是尝试对视图控制器的“viewDidLoad”进行修改。尝试更改“viewDidLayoutSubviews”中的框架。
Old question, but in my case the problem was trying to make the modifications in "viewDidLoad" for the view controller. Try changing the frame in "viewDidLayoutSubviews".
是的,你做的是正确的事。像这样使用的
contentMode
应该使图像大小保持在您的视图中,并保留宽高比。显然,更改框架应该重新定位并调整视图大小。你确定没有其他的东西是可疑的吗?由于某种原因
myImageView
是否为零? (如果它是界面构建器创建的视图,则插座是否已连接?)Yes, you're doing the right thing.
contentMode
used like this should keep the image sized into your view with aspect preserved. And obviously changing the frame should relocate and resize the view.Are you sure that nothing else is fishy? Is
myImageView
nil for some reason here? (If it's an interface builder-created view, is the outlet hooked up?)您可能认为这可行,但世界各地都有人遇到同样的问题。我唯一一次看到它工作不是通过自动缩放,而是通过实际调整大小和重新绘制图像: 如何按比例缩放 UIImageView?
You'd think that'd work, but there are people everywhere having the same issue. The only time I've seen it work isn't by auto-scaling, it's by actually resizing and redrawing the image: How to scale a UIImageView proportionally?
如何将 UIImageView 的大小调整为适合底层图像而不移动它?
有答案。
-T-
How to resize a UIImageView to fit the underlying image without moving it?
Has the answer.
-T-
试试这个:
myImageView.bounds = CGRectMake(0, 0, 30, 30);
我没有运气改变 UIImageView 的框架来缩放图像。尝试更改边界。
Try this:
myImageView.bounds = CGRectMake(0, 0, 30, 30);
I have not had luck with changing the frame of a UIImageView in order to scale the image. Try changing the bounds instead.