UITableViewCell's imageView fit to 40x40

2019-01-13 03:35发布

I use the same big images in a tableView and detailView. Need to make imageView filled in 40x40 when an imags is showed in tableView, but stretched on a half of a screen. I played with several properties but have no positive result:

[cell.imageView setBounds:CGRectMake(0, 0, 50, 50)];
[cell.imageView setClipsToBounds:NO];
[cell.imageView setFrame:CGRectMake(0, 0, 50, 50)];
[cell.imageView setContentMode:UIViewContentModeScaleAspectFill];

I am using SDK 3.0 with build in "Cell Objects in Predefined Styles".

7条回答
ら.Afraid
2楼-- · 2019-01-13 04:04

I put Ben's code as an extension in my NS-Extensions file so that I can tell any image to make a thumbnail of itself, as in:

UIImage *bigImage = [UIImage imageNamed:@"yourImage.png"];
UIImage *thumb = [bigImage makeThumbnailOfSize:CGSizeMake(50,50)];

Here is .h file:

@interface UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size;
@end

and then in the NS-Extensions.m file:

@implementation UIImage (PhoenixMaster)
- (UIImage *) makeThumbnailOfSize:(CGSize)size
{
    UIGraphicsBeginImageContextWithOptions(size, NO, UIScreen.mainScreen.scale);
    // draw scaled image into thumbnail context
    [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();        
    // pop the context
    UIGraphicsEndImageContext();
    if(newThumbnail == nil) 
        NSLog(@"could not scale image");
    return newThumbnail;
}

@end
查看更多
Juvenile、少年°
3楼-- · 2019-01-13 04:05

you might be able to use this?

yourTableViewController.rowImage = [UIImage imageNamed:@"yourImage.png"];

and/or

cell.image = yourTableViewController.rowImage;

and if your images are already 40x40 then you shouldn't have to worry about setting bounds and stuff... but, i'm also new to this, so, i wouldn't know, haven't played around with Table View row/cell images much

hope this helps.

查看更多
Deceive 欺骗
4楼-- · 2019-01-13 04:16

I thought Ben Lachman's suggestion of generating thumbnails in advance rather than on the fly was smart, so I adapted his code so it could handle a whole array and to make it more portable (no hard-coded property names).

- (NSArray *)arrayOfThumbnailsOfSize:(CGSize)size fromArray:(NSArray*)original {
    NSMutableArray *temp = [NSMutableArray arrayWithCapacity:[original count]];
    for(UIImage *image in original){
        UIGraphicsBeginImageContext(size);
        [image drawInRect:CGRectMake(0,0,size.width,size.height)];
        UIImage *thumb = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        [temp addObject:thumb];
    }
    return [NSArray arrayWithArray:temp];
}
查看更多
叼着烟拽天下
5楼-- · 2019-01-13 04:16

Try setting UIImageView.autoresizesSubviews and/or UIImageView.contentStretch.

查看更多
甜甜的少女心
6楼-- · 2019-01-13 04:18

I cache a thumbnail version since using large images scaled down on the fly uses too much memory.

Here's my thumbnail code:

- (UIImage *)thumbnailOfSize:(CGSize)size {
    if( self.previewThumbnail )
        return self.previewThumbnail; // returned cached thumbnail

    UIGraphicsBeginImageContext(size);

    // draw scaled image into thumbnail context
    [self.preview drawInRect:CGRectMake(0, 0, size.width, size.height)];

    UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();    

    // pop the context
    UIGraphicsEndImageContext();

    if(newThumbnail == nil) 
        NSLog(@"could not scale image");

    self.previewThumbnail = newThumbnail;

    return self.previewThumbnail;
}

Just make sure you properly clear the cached thumbnail if you change your original image (self.preview in my case).

查看更多
走好不送
7楼-- · 2019-01-13 04:18

I was able to make this work using interface builder and a tableviewcell. You can set the "Mode" properties for an image view to "Aspect Fit". I'm not sure how to do this programatically.

查看更多
登录 后发表回答