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?
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
...
}
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?)
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?
How to resize a UIImageView to fit the underlying image without moving it?
Has the answer. -T-
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With