简体   繁体   中英

Lazy Loading UICollectionView Images

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM