如何使用Asynchronous GCD为UITableviewcell加载图像/

时间:2014-01-16 07:30:13

标签: ios iphone objective-c

我正在使用GCD异步加载我的uitableview图像,但是有一个问题 - 当向下滚动UITableview时,图像会消失在顶部。有时图像不会显示在UITableviewcell中。这是我的代码。请给我任何想法。我的代码中有任何错误。请帮助我。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{    
static NSString *identifier=@"cell";   
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
 if (cell == nil)
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"identifier"];  
 }
image=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 80)];

[cell.contentView addSubview:image];

 str=att.classimage;

url=@"My URL";

if(str!=nil){

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);

    dispatch_async(queue, ^(void) {
        dispatch_async(dispatch_get_main_queue(), ^{
           NSString *imageStr=[url stringByAppendingString:str];
           image.image = [UIImage imageNamed:imageStr];               
         });      
    });
    image.image=[UIImage imageNamed:@"nav-bar-logo.png"];   
}
return cell;        
}

4 个答案:

答案 0 :(得分:0)

您可以查看link。我已经提供了一个如何使用它的例子,它可以解决你遇到的问题。

这是一个图片下载器类。

答案 1 :(得分:0)

在设置图像之前检查该单元格是否仍在内存中。

UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
if (updateCell)
{
    yourImageView.image = [imagePost fixOrientation];
}

我正在做类似的事情,但是使用NSURLConnection。

NSURL *postImage  =   [NSURL URLWithString:@"Your URL"];

NSMutableURLRequest *imageRequest    =   [[NSMutableURLRequest alloc]initWithURL:postImage cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
[imageRequest setHTTPMethod:@"GET"];

[NSURLConnection sendAsynchronousRequest:imageRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
 {
     imagePost = [UIImage imageWithData:data]; // UIImage Object

     UITableViewCell *updateCell = (id)[tableView cellForRowAtIndexPath:indexPath];
     if (updateCell)
     {
         postImageView.image = [imagePost fixOrientation];// setting my ImageView
     }
 }];

希望这会对你有所帮助。

答案 2 :(得分:0)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    

    static NSString *identifier=@"cell";   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
       cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];  
       UIImageView *imageView =[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 80)];
       imageView.tag = 100;
       [cell.contentView addSubview:imageView];    
    }

   str=att.classimage;
   url=@"My URL";
   UIImageView *cellImageView = [cell.contentView viewWithTag:100];
   cellImageView.image = nil;
   if(str!=nil) {
   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
   dispatch_async(queue, ^(void) {
      NSString *imageStr = [url stringByAppendingString:str];
      UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageStr]]];
      dispatch_async(dispatch_get_main_queue(), ^{            
        cellImageView.image = image;               
      });      
    });
   }
   return cell;        
 }

答案 3 :(得分:0)

您可以使用AFNetworknig来显示远程网址中的图片。

get AFNetworking from here

只是

#import "AFNetworking.h"
    [imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"placeHolderImage"]];