1

I didn't know that uicollection view always calls the method (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

When you move the cells, so my program then always calls loading of images from url. Making it slow.

Master Stroke
  • 5,108
  • 2
  • 26
  • 57
East Bay Ray
  • 81
  • 1
  • 9

3 Answers3

4

You can use SDWebImage for your purpose with cache support. You need not to be download a image that already fetched before.

Just visit https://github.com/rs/SDWebImage and use it.

Lalit Kumar
  • 121
  • 1
  • 3
1

From Apple's Documentation...

– dequeueReusableCellWithReuseIdentifier:forIndexPath:

Call this method from your data source object when asked to provide a new cell for the collection view. This method dequeues an existing cell if one is available or creates a new one based on the class or nib file you previously registered.

Important: You must register a class or nib file using the registerClass:forCellWithReuseIdentifier: or registerNib:forCellWithReuseIdentifier: method before calling this method.

EDIT: I assume you're loading the images asynchronously if not then use the following code.

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
  UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
  [NSURL URLWithString:@"ur image url"]]]];
  dispatch_sync(dispatch_get_main_queue(), ^{
     [[cell imageView] setImage:image];
     [cell setNeedsLayout];
  });
  });

EDIT 2: atomk's suggestion(below comment) makes sense,so i won't be posting the code here.In replacement you can use the library called SDWebImage.You can load as many images you want and the same URL won't be downloaded several times as per the author of the code.

Master Stroke
  • 5,108
  • 2
  • 26
  • 57
  • @atomk.thanks for the NOTE...i will edit the answer now... – Master Stroke Aug 28 '13 at 04:38
  • @atomk Really? Thought the whole point of GCD was to abstract away thread creation in such a way. Do you have any further reading on that? – Carl Veazey Aug 28 '13 at 04:48
  • @atomk..is there any documentation available on that?...i too have the feeling like GCD will take away such things... – Master Stroke Aug 28 '13 at 04:52
  • 1
    `dataWithContentsOfURL` is blocking, so until its done, its thread is blocked, which means GCD has to create more threads if its trying to perform 50 blocking calls at once. `sendAsynchronousRequest:queue:completionHandler:` is GCDs abstraction so that you can use the threads run loop to perform multiple request asynchronously on the same thread. – atomkirk Aug 28 '13 at 04:53
  • @atomk..any links on the same is greatly appreciated...thanks – Master Stroke Aug 28 '13 at 04:56
  • http://stackoverflow.com/questions/14685623/issue-with-gcd-and-too-many-threads – atomkirk Aug 28 '13 at 05:05
  • @BaloniBoi..welcome..Please do accept it as the answer in case it worked for you...thanks... – Master Stroke Aug 28 '13 at 05:33
1

Load your images using [NSURLConnection sendAsynchronousRequest:queue:completionHandler: then use NSCache to prevent downloading the same image over and over. Look here for an example, you may even want to include this AFNetworking library and use it: https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/UIImageView%2BAFNetworking.m

atomkirk
  • 3,701
  • 27
  • 30