加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ios – 在uicollectionview中加载延迟

发布时间:2020-12-14 18:07:11 所属栏目:百科 来源:网络整理
导读:这里是我的collectionview的代码它显示记录,但加载真的请告诉我如何实现延迟加载在这我还有一个占位符pic在我的项目 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { Coll
这里是我的collectionview的代码它显示记录,但加载真的请告诉我如何实现延迟加载在这我还有一个占位符pic在我的项目

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

现在它工作但很慢.

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读