Lazy loading in uicollectionview

here is the code for my viewing the collection, it shows the entries, but the download is really, please tell me how I can implement lazy loading about this. I also have a placeholder file in my project

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MJCell" forIndexPath:indexPath];

// Setup image name
    NSString *url = [[rssOutputData objectAtIndex:indexPath.row]xmllink];
    UIImage *img = nil;
    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    img = [[UIImage alloc] initWithData:data];

    cell.MJImageView.image = img;
    return cell;
}

Now it works, but very slowly.

+4
source share
4 answers

It is very easy to do lazy loading using GCD.

    // Create a queue for the operations
    dispatch_queue_t queue = dispatch_queue_create("photoList", NULL);

    // Start getting the data in the background
    dispatch_async(queue, ^{
        NSData* photoData = [NSData dataWithContentsOfURL:[NSURL URLWithString:object.photoURL]];
        UIImage* image = [UIImage imageWithData:photoData];

        // Once we get the data, update the UI on the main thread
        dispatch_sync(dispatch_get_main_queue(), ^{
           cell.photoImageView.image = image;
        });
    });
+7
source

The easiest way to implement this is to use the SDWebImage library , it does what you need. There is a category UIImageViewthat will allow you to change the code for this:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MJCell" forIndexPath:indexPath];

    // Setup image name
    NSString *url = [[rssOutputData objectAtIndex:indexPath.row]xmllink];
    [cell.MJImageView sd_setImageWithURL:[NSURL URLWithString:url]];
    return cell;
}
+3
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: @"url.com"]];

UIActivityIndicatorView * indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[yourImageView addSubview:indicator];
indicator.center = yourImageView.center;
[indicator startAnimating];
[yourImageView setImageWithURLRequest:request
                     placeholderImage:[UIImage imageNamed:@"placeholder.png"]
                     success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                              dispatch_async(dispatch_get_main_queue(), ^(void){
                                        yourImageView.image = image;
                               });

                     [indicator stopAnimating];
                     [indicator removeFromSuperview];

} failure:nil];
0

Perhaps because your image is so large you can use NSThread to load

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    CollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MJCell" forIndexPath:indexPath];
    cell.tag = indexPath.row; //For index
    [NSThread detachNewThreadSelector:@selector(loadImage:) toTarget:self withObject:cell];
    return cell;
}

- (void) loadImage:(CollectionViewCell *)cell {
  NSString *url = [[rssOutputData objectAtIndex:cell.tag]xmllink];
    UIImage *img = nil;
    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
    img = [[UIImage alloc] initWithData:data];

    cell.MJImageView.image = img;
}
-1
source

All Articles