10

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?

sol
  • 6,402
  • 13
  • 47
  • 57

5 Answers5

3

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
    ...   
}
John
  • 31
  • 1
2

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

Ben Zotto
  • 70,108
  • 23
  • 141
  • 204
  • it's definitely not nil, as it appears on the screen and I can set clipsToBounds on it. – sol Sep 10 '10 at 23:21
1

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.

bbrodjes
  • 66
  • 2
1

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?

Community
  • 1
  • 1
Brent
  • 23,354
  • 10
  • 44
  • 49
1

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

Has the answer. -T-

Community
  • 1
  • 1