UITableViewCell's imageView fit to 40x40

前端 未结 7 1805
离开以前
离开以前 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).

提交回复
热议问题