user1011291
user1011291

Reputation: 109

hidden images when scroll uitableview in iphone

I am doing this code

but when i scroll the uitableview image will disappear what i want i want when image once load then it will not disapper.

Please tell me what can i do in this.

__block NSData *image = nil;
dispatch_queue_t imageQueue = dispatch_queue_create("queueForCellImage", NULL);
dispatch_async(imageQueue, ^{
    NSString *thumbnailURL = @"http://www.bizfy.com/demo/biscoot/a1.png";
    image = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:thumbnailURL]];
    dispatch_async(dispatch_get_main_queue(), ^{
        cell.imageView.image = [UIImage imageWithData:image];
        [cell setNeedsLayout];
    });
});
dispatch_release(imageQueue);

Upvotes: 2

Views: 534

Answers (2)

Rob
Rob

Reputation: 437632

Rather than holding previously downloaded images in NSMutableDictionary or NSMutableArray, I'd suggest using NSCache. It provides a similar interface as NSMutableDictionary (e.g. has a setObject:forKey: method), but you can also configure how many images should be cached (via the countLimit or other mechanisms). You might also want to add an observer for UIApplicationDidReceiveMemoryWarningNotification to empty the cache upon memory pressure.

If you want to take it to the next level, when downloading images used in UITableView or UICollectionView, I'd suggest making sure you (a) prioritize visible images over images that may have scrolled off screen; (b) make sure you constrain the degree of concurrency to avoid timeout problems that can plague GCD global queues, etc. The simplest way to achieve that (as well as the NSCache behavior) is to use one of the established asynchronous image retrieval UIImageView categories, such as those included in AFNetworking or SDWebImage (or implement your own variation of the idea).

Upvotes: 2

Natan R.
Natan R.

Reputation: 5181

You should store the images in an NSArray, NSDictionary or even in the file system. Instead of only setting cell.imageView, try to store in a NSMutableArray or save to the disk, and in the next time cellForRowAtIndexPath is called, you access the image where you stored it.

Some code with the NSMutableDictionary solution, assuming you are using ARC (try to understand):

In your interface file, you declare a NSMutableDictionary *dictionaryImages;

In your implementation file, initiate it in viewDidLoad method: dictionaryImages = [NSMutableDictionaryImages dictionary];

Now, where you download your images, you also should insert the image in your dictionary (I'm not posting all your code, just the relevant parts from that piece)

NSString *thumbnailURL = @"http://www.bizfy.com/demo/biscoot/a1.png";
UIImage *imageForDictionary = [UIImage imageWithData:image];
[dictionaryImages setObject:imageForDictionary forKey:thumbnailURL];

And then, when cellForRowAtIndexPath is called, you associate it to the image (of course, if you have access to the thumbnailURL):

NSString *thumbnailURL = **get it from somewhere**;
UIImage *imageFromDictionary = [dictionaryImages objectForKey:thumbnailURL];
cell.imageView.image = imageFromDictionary;

Upvotes: 1

Related Questions