UITableViewCell's imageView fit to 40x40

前端 未结 7 1785
离开以前
离开以前 2020-12-04 16:14

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

相关标签:
7条回答
  • 2020-12-04 16: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).

    0 讨论(0)
  • 2020-12-04 16:19

    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.

    0 讨论(0)
  • 2020-12-04 16:20

    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.

    0 讨论(0)
  • 2020-12-04 16:29

    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];
    }
    
    0 讨论(0)
  • 2020-12-04 16:31

    I have mine wrapped in a UIView and use this code:

    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight;
    [self addSubview:imageView];
    imageView.frame = self.bounds;
    

    (self is the wrapper UIView, with the dimensions I want - I use AsyncImageView).

    0 讨论(0)
  • 2020-12-04 16:34

    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
    
    0 讨论(0)
提交回复
热议问题