How to resize images in UITableViewCell?

自作多情 提交于 2020-01-07 07:41:18

问题


I am trying to make an Xcode app that has a list that loads data from a JSON file. It basically has a title, subtitle, and a thumbnail image. When the app loads up, the images are exactly as big as the cells. I would either like to resize the images to a set size, or make some extra room in-between the cells. Here is my current code:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *CellIdentifier = @"TableCell";


UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];


NSDictionary *tempDictionary= [self.googlePlacesArrayFromAFNetworking objectAtIndex:indexPath.row];

NSURL *url = [NSURL URLWithString:[tempDictionary objectForKey:@"icon"]];
NSData *data = [NSData dataWithContentsOfURL:url];
cell.imageView.image = [UIImage imageWithData:data];
CALayer * l = [cell.imageView layer];
[l setCornerRadius:13];
[l setBorderWidth:0.5];
[l setBorderColor:[[UIColor lightGrayColor] CGColor]];
cell.imageView.layer.masksToBounds = YES;
...}

Thanks for your help.

Update: I have discovered this:

 UIImage *thumbnail = [UIImage imageWithData: [NSData dataWithContentsOfURL :url]];

CGSize itemSize = CGSizeMake(30, 30);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(30.0, 30.0, itemSize.width, itemSize.height);
[thumbnail drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

The image resizes, but it is just blank.


回答1:


I was facing same problem with image re-sizing. After searching on internet I found a useful method:

+(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

This will return the image size. In my case I made a Helper class and in Helper.h I declared this method as a class method so that I can call it in my UITableView. An than in Helper.m I implemented this method.

In cellForRowAtIndexPath method:

cell.imageView.image = [Helper imageWithImage:***Your Image here*** scaledToSize:CGSizeMake(ImageWidth,ImageHeight)];

Don't forget to import Helper.h in your TableView class. You can also use this class in your way. Hope it works.




回答2:


Try this:

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

Or:

cell.imageView.contentMode = UIViewContentModeScaleAspectFill;

Or, you could scale the images that you get from the url.

CGFloat scale = 10.0f;    // adjust this number
UIImage *image = [UIImage imageWithData:
                                  [NSData dataWithContentsOfURL:url]
                                  scale:scale];
cell.imageView.image = image;


来源:https://stackoverflow.com/questions/25214290/how-to-resize-images-in-uitableviewcell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!