iOS - 是否应该更改 UIImageView 的框架来调整其图像大小?

发布于 2024-09-18 07:25:41 字数 412 浏览 9 评论 0原文

有人能明确回答这个问题吗?似乎有时更改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夜灵血窟げ 2024-09-25 07:25:41

老问题,但就我而言,问题是尝试对视图控制器的“viewDidLoad”进行修改。尝试更改“viewDidLayoutSubviews”中的框架。

- (void)viewDidLayoutSubviews
{
    //do uiimageview frame changing here
    ...   
}

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".

- (void)viewDidLayoutSubviews
{
    //do uiimageview frame changing here
    ...   
}
白云不回头 2024-09-25 07:25:41

是的,你做的是正确的事。像这样使用的 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?)

晒暮凉 2024-09-25 07:25:41

您可能认为这可行,但世界各地都有人遇到同样的问题。我唯一一次看到它工作不是通过自动缩放,而是通过实际调整大小和重新绘制图像: 如何按比例缩放 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?

烟若柳尘 2024-09-25 07:25:41

试试这个:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文