在uicollectionview中加载延迟

时间:2015-03-23 07:12:17

标签: ios objective-c xcode uicollectionview uicollectionviewcell

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

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

4 个答案:

答案 0 :(得分:7)

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

答案 1 :(得分:3)

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

答案 2 :(得分:0)

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

答案 3 :(得分:-1)

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