我正在開發照片庫,爲此,我決定使用基本上在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
?
我也試過,但它也沒有工作:( – Khal