简体   繁体   English

如何在UITableViewCell中调整图像大小?

[英]How to resize images in UITableViewCell?

I am trying to make an Xcode app that has a list that loads data from a JSON file. 我正在尝试制作一个具有从JSON文件加载数据的列表的Xcode应用程序。 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. 图像会调整大小,但只是空白。

Try this: 尝试这个:

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

Or: 要么:

cell.imageView.contentMode = UIViewContentModeScaleAspectFill;

Or, you could scale the images that you get from the url. 或者,您可以缩放从URL获得的图像。

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

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. 就我而言,我创建了一个Helper类,并在Helper.h中将该方法声明为类方法,以便可以在UITableView中调用它。 An than in Helper.m I implemented this method. 我在Helper.m中实现了此方法。

In cellForRowAtIndexPath method: 在cellForRowAtIndexPath方法中:

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

Don't forget to import Helper.h in your TableView class. 不要忘记在TableView类中导入Helper.h。 You can also use this class in your way. 您也可以按自己的方式使用此类。 Hope it works. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM