在UITableView中延遲加載圖像我的約有50個(gè)自定義單元格UITableView。我想在單元格中顯示圖像和標(biāo)簽,我從URL中獲取圖像。我想做一個(gè)懶惰的圖像加載,以便在加載圖像時(shí)UI不會(huì)凍結(jié)。我嘗試在單獨(dú)的線程中獲取圖像,但每次單元格再次可見時(shí)我必須加載每個(gè)圖像(否則重復(fù)使用單元格會(huì)顯示舊圖像)。有人可以告訴我如何復(fù)制此行為。
3 回答

慕標(biāo)琳琳
TA貢獻(xiàn)1830條經(jīng)驗(yàn) 獲得超9個(gè)贊
我建議去NSOperation并在另一個(gè)線程上做你需要的任何事情。
這是我為圖像加載編寫的一個(gè)類:
- (id)initWithTarget:(id)trgt selector:(SEL)sel withImgURL:(NSString *)url { if(self = [super init]) { if(url == nil || [url isEqualToString:@""]) return nil; target = trgt; action = sel; imgURL = [[NSURL alloc] initWithString: url]; } return self;}- (void)main { [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil];}- (void)loadImage { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; UIImage *img = [UIImage imageNamed: @"default_user.png"]; if(![[imgURL absoluteString] isEqualToString: @"0"]) { NSData *imgData = [NSData dataWithContentsOfURL: imgURL]; img = [UIImage imageWithData: imgData]; } if([target respondsToSelector: action]) [target performSelectorOnMainThread: action withObject: img waitUntilDone: YES]; [pool release];}- (void)dealloc { [imgURL release]; [super dealloc];}
希望有所幫助!

RISEBY
TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超5個(gè)贊
你可以使用自我圖像按鈕..你可以從github下載自我圖像按鈕文件...添加到你的項(xiàng)目....
在你的xib中的圖像視圖中更改類“自我圖像按鈕”...
延遲加載稱為同步請(qǐng)求..
自我形象稱為異步請(qǐng)求。自我形象不要等待回應(yīng)..一次顯示所有圖像..
- 3 回答
- 0 關(guān)注
- 596 瀏覽
添加回答
舉報(bào)
0/150
提交
取消