我正在开发照片库,为此,我决定使用基本上在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