UITableViewCell上的图像显示
问题描述:
我想在UITableViewCell中显示图像,我正在使用Web服务来获取图像名称。这是我在Web服务的响应中获得的。UITableViewCell上的图像显示
"2514466130834f61a9g38d57h0e2cb-25-ProfilePic.PNG",
"26144482598634297156heb8c0dafg-26-ProfilePic.PNG",
"2714464392629h60253gd4ec8b1f7a-27-ProfilePic.PNG",
"2814449113316a7gh5db0e2139c84f-28-ProfilePic.PNG",
"2914454043856e3519ah0dbf27cg84-29-ProfilePic.PNG"
上述响应以数组格式保存。这些数组对象然后附加在URL上,并且我需要在UIImageView
上显示图像。 请帮帮我。
答
准备你的网址,如下所示。
// Consider this is your prefix url
NSString *urlPrefix = @"https://your_url_prefix/"; // like http://stackoverflow.com/
// Consider this is the array you parsed from the web service
NSArray *resourceArray = @[@"2514466130834f61a9g38d57h0e2cb-25-ProfilePic.PNG", @"26144482598634297156heb8c0dafg-26-ProfilePic.PNG", @"2714464392629h60253gd4ec8b1f7a-27-ProfilePic.PNG", @"2814449113316a7gh5db0e2139c84f-28-ProfilePic.PNG", @"2914454043856e3519ah0dbf27cg84-29-ProfilePic.PNG"];
准备资源URL在您的tableView的数据源方法:cellForRowAtIndexPath
NSString *resourceURL = [urlPrefix stringByAppendingString:[resourceArray objectAtIndex:indexPath.row]];
,并使用SDWebImage
库加载图像,并设置到您的细胞imageView
像
[cell.imageView setImageWithURL:resourceURL];
注:SDWebImage
lib可以从SDWebImage Git下载 将这个库文件添加到您的项目中,导入像下面一样使用。
#import "UIImageView+WebCache.h"
答
首先下载异步类Here
然后把你的.m文件
#进口 “AsyncImageView.h”
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *[email protected]"simpletableidentifier";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:simpletableidentifier];
if(cell == nil)
{
cell.backgroundColor=[UIColor clearColor];
cell = [[UITableViewCell alloc ]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpletableidentifier];
}
AsyncImageView *Galleryimg = [[AsyncImageView alloc]initWithFrame:CGRectMake(0,0,40,40)];
Galleryimg.contentMode = UIViewContentModeScaleAspectFill;
Galleryimg.clipsToBounds=YES;
Galleryimg.tag=i;
Galleryimg.imageURL=[NSURL URLWithString:[Yourstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[scroll addSubview:Galleryimg];
return cell;
}
希望这将帮助你很大你感觉更好,你的代码运行良好。
如果你喜欢我的回答使接受并给予好评我的答案
你有一个返回图像文件名列表的web服务?你想下载这些图像并将它们显示在表格中?这个任务有很多部分。你已经完成了哪些,你有哪些困难? – Avi
您是否使用默认单元格的自定义单元?并请分享代码,以便我可以更好地帮助您... –