UICollectionView非常慢

时间:2012-11-23 10:53:13

标签: ios uicollectionview

我正在开发一个照片库,为此,我决定使用PSTCollectionView(https://github.com/steipete/PSTCollectionView),它基本上在iOS6设备上实现UICollectionView,并为其他iOS使用自定义视图。我的设备运行的是iOS6。

我使用MKNetworkKit从我的服务器加载图像,并使用缓存的响应来设置图像。

问题是,只要我以最低速度滚动,滚动就会变得非常迟缓(我的猜测是由于图像设置)。看起来,出列的单元格几乎大部分时间都不在右侧indexPath,因此每次都会重新创建图像。 是不是有办法使单元格为好的索引路径出列并从内存(或缓存)重新加载而不是重新映射整个图像?

以下是一些代码:

网格(即UICollectionView)分配

PSUICollectionViewFlowLayout *layout = [[PSUICollectionViewFlowLayout alloc] init];
_gridView = [[PSUICollectionView alloc] initWithFrame:[self.view bounds] collectionViewLayout:layout];
_gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_gridView.delegate = self;
_gridView.dataSource = self;
[_gridView registerClass:[ImageGridCell class] forCellWithReuseIdentifier:CollectionViewCellIdentifier];
[_gridView setAlwaysBounceVertical:YES];

cellForRowAtIndexPath:

ImageGridCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionViewCellIdentifier forIndexPath:indexPath];
id photoId = [self.photoIds objectAtIndex:indexPath.row];
[cell setPhotoId:photoId];
return cell;

setPhotoId方法:

if (![_photoId isEqualToNumber:photoId])
{
    NSLog(@"set Photo ID %@ for old id %@", photoId, _photoId);
    _photoId = photoId;
    self.image.image = nil;
    __block BOOL imageAlreadySetFromCache = NO;
    self.operation = [[BDRWebService manager] downloadImageWithId:_photoId.stringValue
                                                      isThumbnail:YES
                                                completionHandler:^(UIImage *fetchedImage, NSURL *url, BOOL isInCache) {
                                                    if (isInCache && !imageAlreadySetFromCache)
                                                    {
                                                        self.image.image = fetchedImage;
                                                        NSLog(@"image ready %@ (from cache %d) %@", _photoId, isInCache, NSStringFromCGSize(self.image.image.size));
                                                        imageAlreadySetFromCache = YES;
                                                    }
                                                    else if (!imageAlreadySetFromCache)
                                                    {
                                                        NSLog(@"image ready %@ (from cache %d) %@", _photoId, isInCache, NSStringFromCGSize(self.image.image.size));
                                                        self.image.image = fetchedImage;
                                                    }
                                                } errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
                                                }];

}
else
{
    NSLog(@"same cell and image...");
}

这是日志:

2012-11-23 11:39:25.431 cellForRow 12
2012-11-23 11:39:25.433 set Photo ID 269 for old id (null)
2012-11-23 11:39:25.436 cellForRow 13
2012-11-23 11:39:25.437 set Photo ID 268 for old id (null)
2012-11-23 11:39:25.440 cellForRow 14
2012-11-23 11:39:25.441 set Photo ID 267 for old id (null)
2012-11-23 11:39:25.463 cellForRow 15
2012-11-23 11:39:25.468 set Photo ID 266 for old id 280
2012-11-23 11:39:25.480 cellForRow 16
2012-11-23 11:39:25.488 set Photo ID 265 for old id 281
2012-11-23 11:39:25.490 cellForRow 17
2012-11-23 11:39:25.504 set Photo ID 264 for old id 279
2012-11-23 11:39:25.511 cellForRow 18
2012-11-23 11:39:25.566 set Photo ID 263 for old id 276
2012-11-23 11:39:25.570 cellForRow 19
2012-11-23 11:39:25.580 set Photo ID 262 for old id 277
2012-11-23 11:39:25.583 cellForRow 20
2012-11-23 11:39:25.591 set Photo ID 261 for old id 278
2012-11-23 11:39:25.654 cellForRow 21
2012-11-23 11:39:25.659 set Photo ID 260 for old id 273
2012-11-23 11:39:25.661 cellForRow 22
2012-11-23 11:39:25.665 set Photo ID 259 for old id 274
2012-11-23 11:39:25.667 cellForRow 23
2012-11-23 11:39:25.671 set Photo ID 258 for old id 275
2012-11-23 11:39:25.690 cellForRow 24
2012-11-23 11:39:25.693 set Photo ID 257 for old id 270
2012-11-23 11:39:25.696 cellForRow 25
2012-11-23 11:39:25.700 set Photo ID 256 for old id 271
2012-11-23 11:39:25.703 cellForRow 26
2012-11-23 11:39:25.707 set Photo ID 255 for old id 272
2012-11-23 11:39:25.733 cellForRow 27
2012-11-23 11:39:25.737 set Photo ID 254 for old id 269
2012-11-23 11:39:25.740 cellForRow 28
2012-11-23 11:39:25.747 set Photo ID 253 for old id 267
2012-11-23 11:39:25.758 cellForRow 29
2012-11-23 11:39:25.776 set Photo ID 252 for old id 268
2012-11-23 11:39:25.872 cellForRow 30
2012-11-23 11:39:25.886 set Photo ID 251 for old id 264
2012-11-23 11:39:25.890 cellForRow 31
2012-11-23 11:39:25.894 set Photo ID 250 for old id 265
2012-11-23 11:39:25.897 cellForRow 32
2012-11-23 11:39:25.906 set Photo ID 249 for old id 266

它永远是这样的(也许这是适当的行为。毕竟,这就是出列的所有权利?),但我可以让滚动更快。

我可以使用各种提示(在0.2秒后用计时器加载图像并在prepareForReuse方法中使其无效,取消操作(我试过,到目前为止没有更好)),但它不会很好

如何让图片“贴”到单元格以防止每次都有新的分配self.image.image = fetchedImage

2 个答案:

答案 0 :(得分:0)

我认为该块将在另一个线程中执行。所以所有与UI相关的操作都应该在主线程上进行

dispatch_async(dispatch_get_main_queue(), ^{


      if (isInCache && !imageAlreadySetFromCache)
          {
            self.image.image = fetchedImage;
            NSLog(@"image ready %@ (from cache %d) %@", _photoId, isInCache, NSStringFromCGSize(self.image.image.size));
            imageAlreadySetFromCache = YES;
          }
         else if (!imageAlreadySetFromCache)
         {
           NSLog(@"image ready %@ (from cache %d) %@", _photoId, isInCache, NSStringFromCGSize(self.image.image.size));
           self.image.image = fetchedImage;
         }

});  

答案 1 :(得分:0)

您需要使用PSCollectionViewFlowLayout_代替PSUICollectionViewFlowLayoutPSCollectionViewController_代替PSUICollectionViewController等。

我认为这是因为您使用PSTCollecitonView控件。如果您需要原样支持iOS 5.1 PSTCollectionView,但在iOS 6上您需要使用UICollectionView本机。 在这种情况下,可能是iOS 5上的PSTCollectionView。*会很慢。但是在ios 6.0上,一切都应该没问题。

来自官方文件:

  

您想使用UICollectionView,但仍需要支持更旧版本   iOS版本?然后你会喜欢这个项目。原来我   为PSPDFKit编写了它,我的iOS PDF框架支持文本   选择和注释,但这个项目似乎太有用了   其他人为自己保留:)

     

如果你想在iOS4.3 / 5.x上安装PSTCollectionView   iOS6上的UICollectionView,使用PSUICollectionView(基本上添加PS)   任何UICollectionView *类都可以获得旧iOS的自动支持   如果您总是想使用PSTCollectionView,请使用   PSTCollectionView作为类名。 (用PST替换UI)