2014-03-25 5 views
0

UItableViewCell에서 서버의 이미지를로드하고 있습니다.UITableviewcell에서 이미지를로드 할 때 메모리 문제가 발생합니까?

각 이미지 크기가 10MB이므로 메모리에 문제가 발생할 수 있습니다. 나는 tableView

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    locationcellObject=[self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath]; 
    NSDictionary *temp= [sortedArray objectAtIndex:indexPath.row]; 
    locationcellObject.title.text=[temp objectForKey:@"locationtitle"]; 
    locationcellObject.subtitle_Lbl.text=[temp objectForKey:@"category"]; 
    NSString *trimmedtitle = [[temp objectForKey:@"locationtitle"]stringByReplacingOccurrencesOfString:@" " withString:@""]; 
    NSString *name=[NSString stringWithFormat:@"images/%@.png",trimmedtitle]; 
    NSString *imageName=[NSString stringWithFormat:@"http://my_URL_HERE/%@",name]; 
    _tempData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]; 
    UIImage *display=[[UIImage alloc]initWithData:_tempData]; 
    locationcellObject.locationPic_img_View.image=display; 
    locationcellObject.locationPic_img_View.contentMode=UIViewContentModeScaleAspectFit; 

    return locationcellObject; 
} 

을 통해 스크롤 할 때마다 응용 프로그램은 그것을 할 수있는 쉬운 방법이 있나요 충돌?

+0

어디에서 세포를 할당합니까? –

+0

왜 셀에 10MB 이미지를 사용합니까? 어쨌든 (셀 크기가 이미지만큼 크지 않는 한) 크기가 더 작은 이미지가 충분할 것입니다. – giorashc

+0

'Cell'을위한'NSObject'를 만들었고'ViewDidLoad ' – iCoder

답변

0

1), 배경
2) 백그라운드 스레드에서 이미지 다운로드 이미지의 썸네일 크기가 더 작은 (이미지)

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0),^ { 
_tempData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageName]]; 
UIImage *display=[[UIImage alloc]initWithData:_tempData]; 

//make a thumbnail of the image 

UIImage *display; 
CGSize destinationSize = ...; 
UIGraphicsBeginImageContext(destinationSize); 
[originalImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)]; 
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
// 
dispatch_async(dispatch_get_main_queue(), ^{ 
//put result to main thread 
locationcellObject.locationPic_img_View.image= newImage; 
    }); 
}); 
+0

'main_queue' 이미지가'UIImageViewCell'에 표시되지 않습니다. – iCoder

0

을에서 다운로드 할 블록의 이미지를 다운로드해야 . 이렇게하면 두 개의 스레드가 앱 주 스레드와 백그라운드 스레드에서 실행됩니다. 메인 쓰레드의 부하가 줄어 듭니다. 여기이 link을 확인할 수 있습니다

또한

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

    //Call your function or whatever work that needs to be done 
    //Code in this part is run on a background thread 

    dispatch_async(dispatch_get_main_queue(), ^(void) { 

     //Stop your activity indicator or anything else with the GUI 
     //Code here is run on the main thread 

    }); 
}); 

그리고 이미지가 캐시에서로드 될 다음으로 NSCache를 사용하여 캐시에 다운로드 된 이미지를 추가

, 당신은 캐시

에 이미지를 추가하는 방법을 찾을 수 있습니다
0

THIS 자습서를 참조하십시오. UItableView에서 이미지 가져 오기/불러 오기를 효율적으로 설명합니다.

관련 문제