2014-01-16 49 views
-1

我正在使用GCD異步加載我的uitableview的圖像,但存在一個問題 - 向下滾動時,UITableview圖像消失在頂部。有時圖像不顯示在UITableViewCell中。這是我的code.please給我任何想法。我的code.Is有任何錯誤,請幫助我。如何使用異步GCD加載圖像對於UITableviewcell/

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{  
static NSString *[email protected]"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; 

[email protected]"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;   
} 
+0

您正在使用中退出的小區標識爲@ 「細胞」 和標識,而分配/ INITING細胞是@ 「標識」。 – chandu

+0

並修復瞭如何添加imageView。目前它總是添加,而cellForRow被稱爲 – Joshua

+0

我想建議你緩存圖像,如果圖像來自服務器。 – Leena

回答

0

你能檢查這個link。我已經提供了一個如何使用它的例子,它可能會解決您遇到的問題。

這是一個圖像下載類。

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 
    } 
}]; 

希望這會對你有幫助。

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

    static NSString *[email protected]"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; 
    [email protected]"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;   
} 
+0

感謝您的response.But我在cellImageView.image = [UIImage imageWithContentsOfURL:[NSUrl urlWithString:imageStr]]; 。在這一行中,我得到了錯誤,如「沒有選擇器urlWithString的已知類方法」。請幫助我的朋友 – user2930730

+0

請檢查更新的代碼。 – chandu

+0

感謝brother.images會顯示brother.but得到另一個問題。如果我滾動UITableview從頂部到底部。再次滾動底部到頂部的圖像會自動改變。以前顯示的圖像不會顯示。新圖像將顯示 – user2930730

0

您可以使用AFNetworknig來顯示來自遠程URL的圖像。

get AFNetworking from here

只是

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