UITableViewCell imageView suitable for 40x40

I use the same large images in tableView and detailView. It is necessary to make the imageView filled with 40x40 when imags is displayed in the tableView but is stretched to half the screen. I played with several properties, but did not have a 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 the assembly in "Cell Objects in Predefined Styles".

+25
objective-c iphone uitableview uiimageview
Jun 28 '09 at 18:47
source share
7 answers

I put the Ben code as an extension in the NS-Extensions file so that I can show any image to make it a thumbnail, for example:

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

Here is the .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 
+32
Apr 27 '11 at 23:05
source share

I cache the thumbnail since using large images reduced on the fly uses too much memory.

Here is my miniature 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 clean the cache file correctly if you change the original image (self.preview in my case).

+12
Oct 13 '09 at 16:02
source share

I have my shell in a UIView and use this code:

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

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

+4
Sep 09 '09 at 15:07
source share

I thought that Ben Lachman’s proposal for sketching in advance, and not on the fly, was smart, so I adapted his code so that he could process the whole array and make it more portable (without 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]; } 
+2
Mar 24 '11 at 17:59
source share

could you use this?

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

and / or

 cell.image = yourTableViewController.rowImage; 

and if your images are already 40x40, you don’t have to worry about setting borders and so on ... but I am also new to this, therefore, I would not know if I didn’t play with the table View images of rows / cells significantly

hope this helps.

-one
Jun 30 '09 at 16:28
source share

I was able to do this work with the interface designer and tableviewcell. You can set the “Mode” properties for the image to “Fit Aspect”. I am not sure how to do this programmatically.

-one
Jul 31 '09 at 16:17
source share

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

-one
03 Sep '09 at 10:16
source share



All Articles