Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 582
sol Avatar asked Sep 10 '10 23:09

sol


4 Answers

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
    ...   
}
like image 71
John Avatar answered Oct 05 '22 23:10

John


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?)

like image 26
Ben Zotto Avatar answered Oct 05 '22 23:10

Ben Zotto


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?

like image 24
Brent Avatar answered Oct 06 '22 01:10

Brent


How to resize a UIImageView to fit the underlying image without moving it?

Has the answer. -T-

like image 24
Tom Hollins Avatar answered Oct 05 '22 23:10

Tom Hollins