不十分なスクロールに関する UICollectionView の投稿をかなり読んだことがありますが、直接適用されるものはないようであるか、まだ回答がありません。
AFNetworking
各セルに画像 (95 ピクセル四方) を非同期にロードし、画像が再びスクロールして表示されると、画像がキャッシュから復元されます (200 ではなく 0 として指定された応答コードで確認されます) 。
これが私が試したことです:
- 画像が画面に描画されないようにコメントアウトされ
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];
}