8

I have a 16x16 pixel image that I want to display in an UIImageView. So far, no problem, however 16x16 is a bit small so I want to resize the image view to 32x32 and thus also scale the image up. But I can't get it to work, it always shows the image with 16x16, no matter what I try. I googled a lot, and found many snippets here on Stack Overflow, but its still doesn't work. Here is my code so far:

[[cell.imageView layer] setMagnificationFilter:kCAFilterNearest];
[cell.imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[cell.imageView setClipsToBounds:NO];
[cell.imageView setFrame:CGRectMake(0, 0, 32, 32)];
[cell.imageView setBounds:CGRectMake(0, 0, 32, 32)];
[cell.imageView setImage:image];

I don't want to create a new 32x32 pixel image because I already have some memory problems on older devices and creating two images instead of having just one looks like a very bad approach to me (the images can be perfectly scaled and it doesn't matter if they lose quality).

4 Answers 4

29

I have successfully made it using CGAffineTransformMakeScale!

cell.imageView.image = cellImage;
//self.rowWidth is the desired Width
//self.rowHeight is the desired height
CGFloat widthScale = self.rowWidth / cellImage.size.width;
CGFloat heightScale = self.rowHeight / cellImage.size.height;
//this line will do it!
cell.imageView.transform = CGAffineTransformMakeScale(widthScale, heightScale);
4
  • This is a good, albeit duct-tape-y solution if you don't want to subclass UITableViewCell.
    – jdc
    Mar 27, 2012 at 17:54
  • What is cellImage.size.width? I am using initWithStyle method
    – Dejell
    Sep 30, 2013 at 9:18
  • cellImage is a temp UIImageView used to calculate the width and height.
    – Ahmed
    Sep 30, 2013 at 13:10
  • 1
    Great, but you need to set this first cell.imageView.contentMode = UIViewContentModeScaleAspectFit; Apr 8, 2015 at 23:09
4

I think you need to set the contentMode:

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

In context:

UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"slashdot" ofType:@"png"]];
imageView = [[UIImageView alloc] initWithImage:image];
[imageView setBackgroundColor:[UIColor greenColor]];
[imageView setFrame:CGRectMake(x,y,32,32)];
imageView.contentMode = UIViewContentModeScaleAspectFit;    
[self.view addSubview:imageView];

Note: I've set a background colour so you can debug the on-screen boundaries of the UIImageView. Also x and y are arbitrary integer coordinates.

2
  • Thanks for the update, but it still doesn't work. If I create an UIImageView like you did, everything works fine, but the UIImageView from the UITableViewCell behaves different. I tested it also with another background color to verify that also the view itself still has an 16x16 frame.
    – JustSid
    Nov 9, 2010 at 16:26
  • Okay, looks like a UITableViewCell updates the frame anyway, no matter what I do. I'm now creating the labels and the UIImageView by myself and add them to the contentView of the cell.
    – JustSid
    Nov 10, 2010 at 9:48
1

Using CGAffineTransformMakeScele as @ahmed said is valid and do not seems to be duck type solution at al! For instance, if you have a large image and put it into a UITableViewCell (say the image is 2x larger than the one that fits into a table cell. If you scale by 0.9 you don't see any result. Only if you scale by less than 0.5 (because 0.5*2.0 = 1.0 that is the size of the cell). So it seems that inside the api, apple is doing exactly that.

1

You need to override the layoutSubviews method. By default, it's resizing the imageview based on the cell height size.

-(void)layoutSubviews
{
    [super layoutSubviews];

    self.imageView.frame = CGRectMake(self.imageView.frame.origin.x,
                                      self.imageView.frame.origin.y,
                                      MY_ICON_SIZE,
                                      MY_ICON_SIZE);
}

You'll probably want to recalculate the origin as well so it's vertically centered.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.