I have an image caching class that stores profile images in an NSMutableDictionary. If the key exists, the cache returns a UIImage object, if it does not, it downloads it, stores it in the dictionary, and returns the UIImage. This shared cache is called inside a NSThreaded method like this:
[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];
}
The problem is that if they scroll the UITableView before anything is loaded, the images get backlogged and then eventually all the images are loaded and they flash and change on the current cells until it gets to the correct ones. I thought it would be good to create an instance of NSThread in each cell and just cancel the thread in -prepareForReuse but I can't seem to be able to reuse the NSThread object again, any ideas on how to fix this?