xcode uicollectionview 中的延迟加载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29204887/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:51:28  来源:igfitidea点击:

Lazy loading in uicollectionview

iosobjective-cxcodeuicollectionviewuicollectionviewcell

提问by Vix Hunk

here is the code for my collectionview it is showing records but loading really please tell me how can i implement lazy loading on this i also have a placeholder pic in my project

这是我的 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;
}

right now it is working but very very slow.

现在它正在工作,但非常非常慢。

回答by Amit

It's pretty easy to do lazy loading using GCD.

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

回答by Azat

The easiest way to implement that is use SDWebImagelibrary, it does right what you need. There is UIImageViewcategory that will allow you to modify code for that:

实现它的最简单方法是使用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;
}

回答by iShaalan

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

回答by Mr Phuc 87

Maybe, because your image is so large You can use NSThread for loading

也许,因为你的图片太大了 你可以使用 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;
}