我正在开发一个照片库,为此,我决定使用基本上在 iOS6 设备上实现 UICollectionView的 PSTCollectionView ( https://github.com/steipete/PSTCollectionView ),并为其他 iOS 使用自定义视图。我的设备正在运行 iOS6。
我使用 MKNetworkKit 从我的服务器加载图像并使用缓存响应来设置图像。
问题是,一旦我以最低速度滚动,滚动就会变得非常迟钝(我的猜测是由于图像设置)。看起来,出队的单元格几乎大部分时间不在正确的 indexPath 上,因此每次都重新创建图像。有没有办法让单元格为良好的索引路径出列并从内存(或缓存)重新加载而不是重置整个图像?
以下是一些代码:
Grid(即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
每次都进行新的分配?