在 uicollectionview 中延迟加载

Lazy loading in uicollectionview

这是我的 collectionview 的代码,它显示记录但确实加载请告诉我如何实现延迟加载我的项目中也有一个占位符图片

- (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;
}

现在它正在运行,但非常非常慢。

最简单的实现方法是使用 SDWebImage 库,它可以满足您的需要。有 UIImageView 类别可让您为此修改代码:

- (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;
}
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];

可能吧,因为你的图片太大了 您可以使用 NSThread 进行加载

- (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;
}

使用 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;
        });
    });