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

慕標琳琳
TA貢獻1830條經驗 獲得超9個贊
我建議去NSOperation并在另一個線程上做你需要的任何事情。
這是我為圖像加載編寫的一個類:
- (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貢獻1856條經驗 獲得超5個贊
你可以使用自我圖像按鈕..你可以從github下載自我圖像按鈕文件...添加到你的項目....
在你的xib中的圖像視圖中更改類“自我圖像按鈕”...
延遲加載稱為同步請求..
自我形象稱為異步請求。自我形象不要等待回應..一次顯示所有圖像..
- 3 回答
- 0 關注
- 597 瀏覽
添加回答
舉報
0/150
提交
取消