1

There's a UIImageView for every cell of the UICollectionView and I need to load them so that as you scroll through the collection they show up properly and every image loads on its cell at its index path, once.

Every image is downloaded from Kinvey.

Code to download every image:

UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];    

UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0,  cell.bounds.size.width, cell.bounds.size.width)];

[cell addSubview:imageView];    

    [KCSFileStore downloadData:@"id for item at index path"] completionBlock:^(NSArray *downloadedResources, NSError *error) {

        if (error == nil) {

            KCSFile* file = downloadedResources[0];

            dispatch_async(dispatch_get_main_queue(), ^(void){

                imageView.image = [UIImage imageWithData:file.data];
            });

            NSLog(@"Downloaded.");

        } else {

            NSLog(@"Error: %@", error.localizedDescription);
        }

    } progressBlock:nil];

That "dispatch_async(dispatch_get_main_queue(), ^(void)..." is something I tried to solve my problem downloading images asynchronously, but that didn't work.

Thanks in advance.

Hugo
  • 147
  • 5
  • what you want to do exactly? download image in backghround, while scrolling it should appear, isn't it? – Santo Jun 13 '16 at 06:23
  • is this done on cellForRow... if so you will keep adding imageView which will make the view rendering slower and slower as the view keeps piling up – Joshua Jun 13 '16 at 06:56

1 Answers1

0

why you try to download the images. If you want to perform lazy loading for images in collectionView using third party library. then SDWebimages is the best third party library for that.this library not only perform the lazy loading but provide multiple other option also. just take a look.

Hope this will Help You and solve your problem.

JigneshP
  • 344
  • 2
  • 7