Objective c 将图像从URL加载到UITableViewCell';UIImageView
我有以下代码:(注意:Objective c 将图像从URL加载到UITableViewCell';UIImageView,objective-c,ios,ios4,Objective C,Ios,Ios4,我有以下代码:(注意:newsImageURL是一个NSArray) 我正在尝试使用以下代码将这些图像加载到单元格中: NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]]; cell.image = [UIImage imageWithData:imageData]; 当我使用这一行时,图像加载良
newsImageURL
是一个NSArray
)
我正在尝试使用以下代码将这些图像加载到单元格中:
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]];
cell.image = [UIImage imageWithData:imageData];
当我使用这一行时,图像加载良好:
cell.image = [UIImage imageNamed:@"imagename.png"];
我做错了什么?您可以通过以下方式加载数据:
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]];
您也可以这样实例化URL数组:
NSArray *newsImageURL = [imagesURL componentsSeparatedByString:@","];
然而,如果有人在桌子上滚动了很多次,当细胞被回收时,你可能会多次加载图像 您应该使用支持缓存、默认占位符和延迟加载图像的现有框架 是一个好的简单的框架
#import "UIImageView+WebCache.h"
...
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = @"MyIdentifier";
UITableViewCell *cell =
[tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
cell.textLabel.text = @"My Text";
return cell;
}
也许你可以试试。它比SDWebImage简单得多。你只需要两个源文件(ALImageView.h/ALImageView.m)。你可以重用图像视图在tableview单元格中重新加载不同的URL
您可以通过以下代码轻松加载所有图像,Details数组是一个主数组
Details Array :- {
"item_code" = 709;
"item_desc" = Qweqweqwe;
"item_name" = AQA;
"item_photo" = "http://toshaya.com/webapp/snap&sell/api/img_items/709.png";
"item_price" = "0.00";
"item_till" = "20-25";
"item_type" = Orange;
latitude = "";
longitude = "";
}
在以下代码的帮助下,将照片URL检索为字符串
NSString * result = [[DetailArray objectAtIndex:indexPath.row]objectForKey:@"item_photo"]; //componentsJoinedByString:@""];
NSLog(@"%@",result);
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:result]];
cell.icon.image = [UIImage imageWithData:imageData];
你的代码会发生什么变化?是否加载了一些图像,或者每次加载图像时都会得到一个空白区域?当我尝试从URL加载图像时,图像空间消失,没有显示任何图像,基本上什么也没有发生。但是当我使用
imageNamed
加载它时,它会在您调用将图像数据加载到NSData对象imageData之后加载fine,您是否检查过它是否确实包含任何数据[imageData length];好吧,它返回空值,所以我猜这就是问题所在,但我真的不明白为什么!!!网址在那里!!看看你实例化数组的那一行,你说的是[AllNewsHeadingComponentSSE…然而,从这个例子来看,它应该是NSArray*newsImageURL=[imagesURL ComponentSSepartedByString:@],“]他问为什么上面的代码不起作用……我也想知道它……添加一个框架不是正确的答案,也没有帮助
NSString * result = [[DetailArray objectAtIndex:indexPath.row]objectForKey:@"item_photo"]; //componentsJoinedByString:@""];
NSLog(@"%@",result);
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:result]];
cell.icon.image = [UIImage imageWithData:imageData];