【问题标题】:UICollectionView LagUICollectionView 滞后
【发布时间】:2015-07-09 06:12:26
【问题描述】:

我有一个滚动图像的寻呼UICollectionView。每个图像填满屏幕。对于普通照片,我的collectionView 可以流畅地滚动,但是对于全景照片,当我滚动图像时它开始滞后。

- (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 中做点什么吗?。谢谢

【问题讨论】:

    标签: ios uicollectionview lag collectionview


    【解决方案1】:

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

    - (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,然后再次运行。

    我有过这样的经历,有时我什至会检查可能的内存处理错误,但重新启动模拟器可以解决问题。

    【讨论】:

      【解决方案2】:

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

      在 imageCell 中,

      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:

      .....
      cell.file = file
      .....
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多