2012-11-23 293 views
1

我正在開發照片庫,爲此,我決定使用基本上在iOS6設備上實現UICollectionView的PSTCollectionView(https://github.com/steipete/PSTCollectionView),併爲其他iOS使用自定義視圖。我的設備正在運行iOS6。UICollectionView非常慢

我使用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

回答

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

}); 
+0

我也試過,但它也沒有工作:( – Khal

0

您需要使用PSCollectionViewFlowLayout_代替PSUICollectionViewFlowLayoutPSCollectionViewController_代替PSUICollectionViewController等進行

我想這是因爲你使用PSTCollecitonView控制。如果您需要按原樣支持iOS 5.1 PSTCollectionView,但在iOS 6上需要使用UICollectionView本機。 在這種情況下,可能是iOS 5上的PSTCollectionView。*會很慢。但在iOS 6.0上,所有的一切都應該沒問題。

從官方文檔:

你想用UICollectionView,但仍然需要支持舊版本 的iOS?那麼你會喜歡這個項目。本來我 寫它PSPDFKit,我的iOS PDF框架,支持文本 選擇和註釋,但這個項目似乎太有用 他人保持它爲自己:)

如果你想有PSTCollectionView對iOS4的。 3/5.x和 iOS6上的UICollectionView,使用PSUICollectionView(基本上在 的任何UICollectionView *類上添加PS,以獲得舊版iOS的自動支持)。如果您始終想要使用PSTCollectionView,請使用 PSTCollectionView作爲類名稱。 (用PST替換UI)

+0

謝謝,我會看看它。 – Khal