我有一個圖像緩存類,它將配置文件圖像存儲在NSMutableDictionary中。如果密鑰存在,則緩存返回UIImage對象,如果不存在,則下載它,將其存儲在字典中,並返回UIImage。這種共享緩存被稱爲NSThreaded方法中是這樣的:UITableViewCell線程中的圖像緩存
[NSThread detachNewThreadSelector:@selector(setImageForUser:) toTarget:self withObject:imageUrlString];
-(void)setImageForUser:(NSString *)imageUrlString {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
LPImageCacher *imageCache = [LPImageCacher sharedCache];
UIImage *profileImg = [imageCache getCachedImageFromMemory:imageUrlString];
self.userProfileImageView.image = profileImg;
if (profileImg == nil) {
imageUrlString = [[myGlobals sharedInstance].avatarUrlDefault stringByAppendingString:@"/avatar.png"];
self.userProfileImageView.image = [imageCache getCachedImageFromMemory:imageUrlString];
}
[pool release];
}
的問題是,如果他們滾動的UITableView加載任何東西之前,圖像獲得積壓,然後最終所有的圖像加載它們閃爍或改變在當前的單元格上,直到它到達正確的單元格。我認爲在每個單元格中創建一個NSThread實例並且僅僅取消-prepareForReuse中的線程會很好,但我似乎無法再次重用NSThread對象,關於如何解決這個問題的任何想法?