A Stick
A Stick

Reputation: 7

How to scale UIImage in tableview?

This is my code:

UIImage *image1 = [UIImage imageNamed:@"AllMyFiles-1 (dragged).tiff"];
cell.imageView.image = image1;

cell.textLabel.text = self.greekLetters[indexPath.row];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

And this is is what it looks like: tableview with image

And I want the UIImage to be a bit smaller much like this: tableview with smaller image

Upvotes: 0

Views: 43

Answers (1)

Eder Martins
Eder Martins

Reputation: 111

There are some options to solve that...

1) You can put transparency in your image, getting something like that: image centered with transparency

2) You can draw your custom cells prototype on your StoryBoard or Xib file

3) You can resize your image programmatically:

Add this method on your class or a extension of UIImage:

- (UIImage *) reziseImage:(UIImage*) image withSize:(CGSize) newSize {
    UIGraphicsBeginImageContext(newSize);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

And call it on your method:

UIImage *image1 = [UIImage imageNamed:@"AllMyFiles-1 (dragged).tiff"];
cell.imageView.image = image1;
CGSize newSize = CGSizeMake(25, 25);
image1 = [self reziseImage:image1 withSize:newSize];
cell.imageView.image = image1;

cell.textLabel.text = self.greekLetters[indexPath.row];

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

Upvotes: 1

Related Questions