我已经阅读了很多关于滚动不良的 UICollectionView 帖子,但似乎没有一个可以直接应用,或者它们仍然没有得到答复。
我AFNetworking
用来将图像(95 平方像素)异步加载到每个单元格上,然后当图像再次滚动到视图中时,图像会从缓存中恢复(由给出的响应代码验证为 0 而不是 200)。
这是我尝试过的:
- 注释掉,
weakCell.photoView.image = image;
因此图像不会在屏幕上绘制,并且滚动更流畅(在 HTTP 获取期间仍然有点卡顿) - 从方法中删除了所有 AFNetworking 代码,
cellForRowAtIndexPath
滚动更加流畅(即使自定义单元格阴影等仍在屏幕上绘制) - 当我在屏幕上仅绘制单元格视图(带有阴影)时,滚动 100 个单元格时非常流畅。一旦我开始在屏幕上绘制图像,我的设备上的滚动效果就很差,甚至在模拟器上也很明显。Instagram 在他们的个人资料视图上可以非常流畅地滚动数百个单元格,所以我试图接近他们的表现。
有什么方法可以改进下面的任何代码以提高滚动性能?
这是我的手机代码:
#import "PhotoGalleryCell.h"
@implementation PhotoGalleryCell
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Setup the background color, shadow, and border
self.backgroundColor = [UIColor colorWithWhite:0.25f alpha:1.0f];
self.layer.borderColor = [UIColor blackColor].CGColor;
self.layer.borderWidth = 0.5f;
self.layer.shadowColor = [UIColor blackColor].CGColor;
self.layer.shadowRadius = 3.0f;
self.layer.shadowOffset = CGSizeMake(0.0f, 2.0f);
self.layer.shadowOpacity = 0.5f;
// Make sure we rasterize for retina
self.layer.rasterizationScale = [UIScreen mainScreen].scale;
self.layer.shouldRasterize = YES;
// Add to the content view
self.photoView = [[UIImageView alloc] initWithFrame:self.bounds];
[self.contentView addSubview:self.photoView];
}
return self;
}
- (void)prepareForReuse
{
[super prepareForReuse];
self.photoView.image = nil;
self.largeImageURL = nil;
}
这是我的 UICollectionView 代码:
#pragma mark - Collection View Delegates
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [zePhotos count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PhotoGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kPGPhotoCellIdentifier forIndexPath:indexPath];
// Get a reference to the image dictionary
NSDictionary *photoDict = [[zePhotos objectAtIndex:indexPath.row] objectForKey:@"image"];
// Asynchronously set the thumbnail view
__weak PhotoGalleryCell *weakCell = cell;
NSString *thumbnailURL = [[photoDict objectForKey:@"thumbnail"] objectForKey:@"url"];
NSURLRequest *photoRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:thumbnailURL]];
[cell.photoView setImageWithURLRequest:photoRequest
placeholderImage:nil
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
weakCell.photoView.image = image;
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
NSLog(@"Error retrieving thumbnail... %@", [error localizedDescription]);
}];
// Cache the large image URL in case they tap on this cell later
cell.largeImageURL = [[photoDict objectForKey:@"large"] objectForKey:@"url"];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:@"showPhotoDetail" sender:self];
}