views:

203

answers:

4

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?

+1  A: 

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

quixoto
it's definitely not nil, as it appears on the screen and I can set clipsToBounds on it.
sol
A: 

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: http://stackoverflow.com/questions/185652/how-to-scale-a-uiimageview-proportionally/537697#537697

UltimateBrent
A: 

@sol

the fact that you can set properties on a nil object (send messages to nil) in objectiveC does not prove anything. It is in fact normal and the program won't crash, so it might be wiser to watch or print the value.

lupu1001