2014-01-16 2 views
-1

GCD를 사용하여 내 uitableview의 이미지를 비동기 적으로로드하고 있지만 문제가 있습니다 - UITableview를 스크롤 할 때 이미지가 맨 위에 표시되지 않습니다. 때때로 이미지가 표시되지 않습니다. UITableviewcell. 이게 내 코드 야. 생각 좀 해줘. 내 코드에 오류가있어. 제발 도와 줘.비동기 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

당신이 큐에서 제거하는 데 사용하는 셀 식별자는 @ "세포"및 식별자 ALLOC/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

감사합니다.하지만 cellImageView.image에 오류가 있습니다. [UIImage imageWithContentsOfURL : [NSUrl urlWithString : imageStr]]; . 이 줄에서 "선택기 urlWithString에 대한 알려진 클래스 메서드가 없습니다."와 같은 오류가 발생했습니다. 친구 좀 도와주세요 – user2930730

+0

업데이트 된 코드를 확인하십시오. – chandu

+0

thanks brother.images 감사합니다 brother.but 다른 problem.if 내가 위쪽에서 아래쪽으로 UITableview 스크롤합니다. 그리고 다시 내가 맨 아래로 스크롤 이미지가 자동으로 변경됩니다. 이전에 표시된 이미지가 표시되지 않습니다. 새로운 이미지가 표시됩니다 – user2930730

0

AFNetworknig을 사용하면 원격 URL의 이미지를 표시 할 수 있습니다.

get AFNetworking from here

단지

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