2013-03-14 5 views
0

앱의 콘텐츠 로딩에 문제가 있습니다. 앱에서 데이터를 가져 오지만 이미지를로드하는 데 많은 시간이 걸리는 것으로 나타났습니다.로드 할 가능성이 있습니다. afterword 이미지. 코드는 다음과 같습니다 : 당신이 세부 사항을 더 원하는 경우앱이 이미지로드 문제로 인해 속도가 느려짐

NSDictionary *dict=[discussionArray objectAtIndex:indexPath.row]; 
UIImageView *avatarimage = (UIImageView *)[cell viewWithTag:4]; 
NSString *photoStrn=[dict objectForKey:@"photo"]; 

dispatch_async(dispatch_get_global_queue(0,0), ^{ 
        NSString *u=[NSString stringWithFormat:@"http://%@",photoStrn]; 
        NSURL *imageURL=[NSURL URLWithString:u]; 
        NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
        dispatch_sync(dispatch_get_main_queue(), ^{ 
        UIImage *dpImage = [UIImage imageWithData:imageData]; 
        if (dpImage==nil) 
        { 
        dpImage = [UIImage imageNamed:@"profileImage.png"]; 
        } 
        avatarimage.image = dpImage; 

     }); 

나는 :) 제공

+0

이 하나 http://stackoverflow.com/questions/9786018/loading-an-image-into-uiimage-asynchronously – Balu

+0

이가이 모양 가지고 참조 답변 : http://stackoverflow.com/a/15270523/2106940 – Jeremy

답변

4

당신은이 일을 위해 GCD를 사용할 수 있습니다

dispatch_async(dispatch_get_global_queue(0,0), ^{ 
    for (NSDictionary *dic in discussionArray) 
    { 
     NSString *photoStr=[dic objectForKey:@"photo"]; 
     NSString * photoString=[NSString stringWithFormat:@"http://%@",photoStr]; 
     UIImage *dpImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]]; 
     if (dpImage==nil) 
     { 
      dpImage = [UIImage imageNamed:@"profileImage.png"]; 
     } 
    } 
}); 
+0

페이지 이동 속도가 빨라졌습니다.이 방법을 시도해 보니 앱 스크롤 아바타 이미지를 스크롤하고 이미지를 다시로드하는 문제가 하나 있습니다. 나는 약간의 오류가 있다고 생각한다. 위의 코드를 업데이트 중이다. –

+0

@ JamesMitchee 나는 당신이 tableView를 사용하고 있으며, 셀을 재사용 할 때 이미지를 기본값으로 재설정하는 것을 잊어 버린 것 같아요. – Till

+0

@ 시도했지만 성공하지 못했습니다. ( –

0

SDWebImage Here을 얻고 프로젝트에 그것을 추가 포함됩니다

있는 UIImageView + WebCache.h

클래스 구현 파일

UIImageView *imag=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)]; 
[imag setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[[jsonarray objectAtIndex:indexPath.row]valueForKey:@"imgurl"]]] placeholderImage:[UIImage imageNamed:@"[email protected]"]]; 
[self.view addSubview:imag]; 
[imag release]; 

SDWebImage는 URL에서 이미지를로드하는 데 더 유용합니다.

희망이 도움이됩니다!

0

제임스,이 라인으로

[NSData dataWithContentsOfURL:[NSURL URLWithString:photoString]]] 

당신은 주 스레드에서 동기 네트워크 호출을 확인합니다. 현재 스레드는 네트워크 호출이 완료 될 때까지 중단됩니다.

해결책은 비동기 네트워크 호출을 수행하는 것입니다. AFNetworking 라이브러리는 이미지를 비동기 적으로로드하는 데 유용한 카테고리를 제공합니다 (UIImageView+AFNetworking).

관련 문제