从URL加载图像的UITableViewCell(需要加载它们asynchronous)
我有一个自定义的类,通过XMLparsing并获取图像的URLstring(我存储在一个数组中)。 然后我想要检索这些string来加载图像,并显示每个UITableViewCell。 这是我的代码:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; UILabel *labelText = (UILabel *)[cell viewWithTag:1000]; labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"name"]; NSURL *imageURL = [NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageCell"]]; NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage *image = [UIImage imageWithData:imageData]; cell.imageView.image = image; return cell; }
很显然,当我向下滚动桌面时,应用程序会加载更多的图像,当图像加载时,用户将无法做任何事情。 它看起来像应用程序冻结。 我想加载的图像不是很大(大约8kb)。 也许有一种方法,我可以加载和存储在后台? 我也想给用户更多的自由,比如向下滚动,用文本查看单元格,但没有图像。 然后强制应用程序加载用户正在查看的单元格的图像。
有没有办法修改我的代码或任何其他解决scheme,以正确加载来自URLstring的图像?
任何意见,将不胜感激,谢谢。
使用这个asynchronous加载imageview的方式
- (UITableViewCell *)tableView:(UITableView *)_tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; UILabel *labelText = (UILabel *)[cell viewWithTag:1000]; labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"name"]; //get a dispatch queue dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //this will start the image loading in bg dispatch_async(concurrentQueue, ^{ NSURL *imageURL = [NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageCell"]]; NSData *image = [[NSData alloc] initWithContentsOfURL:imageURL]; //this will set the image when loading is finished dispatch_async(dispatch_get_main_queue(), ^{ cell.imageView.image = [UIImage imageWithData:image]; }); }); return cell; }
尝试加载asynchronous的图像,所以主线程不会阻塞。
你的代码如下所示:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; UILabel *labelText = (UILabel *)[cell viewWithTag:1000]; labelText.text = [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"name"]; NSURL *imageURL = [NSURL URLWithString:[[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"imageCell"]]; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; UIImage *image = [UIImage imageWithData:imageData]; dispatch_async(dispatch_get_main_queue(), ^{ cell.imageView.image = image; }); }); return cell; }
您可以使用SDWebImage框架asynchronous加载图像。
请按照以下链接: https : //github.com/rs/SDWebImage
只需将SDWebImage框架添加到您的项目中,并将另一个链接器标志“-ObjC”设置为使用该框架即可。
下载链接: https : //github.com/rs/SDWebImage/releases
下载这个项目,并在你单元格的imageview .. 链接中使用asynimageveiw
使用NSOperation和NSOperationQueue.A问题的完美解决scheme。使用下面的链接,看看它是如何工作的。
http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues
要么
使用AFNetworking库它也提供了一个UIImageview的类别下载图像asynchronous。