简体   繁体   English

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

[英]iOS - Should changing the frame of a UIImageView resize its image?

Can someone answer this definitively? 有人可以肯定地回答吗? It seems that sometimes changing the frame of a UIImageView resizes its image and sometimes it doesn't. 似乎有时更改UIImageView的框架会调整其图像大小,有时却没有。

I have loaded an image and created a UIImageView with it, sized about 100x100. 我已加载图像并使用它创建了一个UIImageView,尺寸约为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. 我得到了图像的30x30裁剪部分,但只有它的上角。 How do I shrink the whole image down to 30x30? 如何将整个图像缩小到30x30?

Old question, but in my case the problem was trying to make the modifications in "viewDidLoad" for the view controller. 老问题了,但是在我的情况下,问题是试图在视图控制器的“ viewDidLoad”中进行修改。 Try changing the frame in "viewDidLayoutSubviews". 尝试在“ viewDidLayoutSubviews”中更改框架。

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

Yes, you're doing the right thing. 是的,您做对了。 contentMode used like this should keep the image sized into your view with aspect preserved. 像这样使用的contentMode应该在保留contentMode将图像调整为适合您的视图的大小。 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? 由于某些原因, myImageView是否myImageView nil? (If it's an interface builder-created view, is the outlet hooked up?) (如果这是界面构建器创建的视图,则插座是否已连接好?)

Try this: 尝试这个:

myImageView. myImageView。 bounds = CGRectMake(0, 0, 30, 30); 界限 = CGRectMake(0,0,30,30);

I have not had luck with changing the frame of a UIImageView in order to scale the image. 为了缩放图像,我还没有改变UIImageView的框架。 Try changing the bounds instead. 尝试更改范围

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?

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

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