首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UICollectionView Lag

UICollectionView Lag
EN

Stack Overflow用户
提问于 2015-07-09 14:12:27
回答 2查看 605关注 0票数 0

我有一个滚动图像的分页UICollectionView。每个图像都充满了整个屏幕。对于普通照片,我的collectionView可以流畅地滚动,但对于全景拍摄,当我滚动图像时,它会开始滞后。

代码语言:javascript
复制
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    imageCell *cell = (imageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];

    cell.tag = indexPath.row;

    PFObject *temp = [_dataArray objectAtIndex:indexPath.row];
    PFUser *user = [temp objectForKey:@"user"];
    PFFile *file = [temp objectForKey:@"image"];

    [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
        if (!error) {
            cell.selectedImageView.image = [UIImage imageWithData:data];
            self.navigationItem.title = [user objectForKey:@"Name"];
        }
    }];

    return cell;
}

如你所见,我在后台加载了图像。

我是否需要在willDisplayCell中执行一些操作?谢谢

EN

回答 2

Stack Overflow用户

发布于 2015-07-09 14:21:45

您每次都在加载数据。尝试一些方法来防止这种情况的发生。

代码语言:javascript
复制
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{
    imageCell *cell = (imageCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    ...

    if (cell.selectedImageView.image == nil)
    {
        [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
            if (!error) {
                cell.selectedImageView.image = [UIImage imageWithData:data];
                self.navigationItem.title = [user objectForKey:@"Name"];
            }
        }];
    }

    return cell;
}

此外,有时它只是在模拟器中。尝试重置模拟器或Xcode,然后再次运行。

我有经验,有时,我甚至检查可能的内存处理错误,但重启模拟器做了工作。

票数 0
EN

Stack Overflow用户

发布于 2015-07-09 14:39:09

我以前从来没有真正使用过Parse对象,但似乎是因为一个cell试图同时加载多个图像。我认为您应该将加载图像的逻辑移动到单元格中,并在prepareForReuse中重用图像时取消图像加载。当您在单元格中加载图像时,通常会使用此方法。我将给你一个快速的例子,希望它能给你一个想法。

在imageCell中,

代码语言:javascript
复制
var file: PFFile? {
    didSet {
        if let f = file {
             [file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
                if (!error) {
                     selectedImageView.image = [UIImage imageWithData:data];
                }
            }];
        }
    }
}

........

override func prepareForReuse() {
    super.prepareForReuse()
    if let f = file {
       f.cancel()
    }
}

在collectionView中: cellForItemAtIndexPath:

代码语言:javascript
复制
.....
cell.file = file
.....
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31309474

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档