0

アプリの UICollectionView に (インターネットからの) 写真のサムネイルを表示しています。あまりにも多くの (20 以上のような) 解像度の高い画像があると、アプリは単純にクラッシュします。メモリ管理に関しては、私は一種のアマチュアです。私の質問は、メモリ管理または画像のスケーリングを介してこの問題を解決する必要があるかどうかです (そうすると、画像が UIViewContentModeScaleAspectFill にないのではないかと疑われます)。

現在、 SDWebImageを使用して画像をロードしています。

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"thumbCell" forIndexPath:indexPath];
    NSString *URL = [self.album.imageURLs objectAtIndex:(indexPath.item+1)];

    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(80, 80, 277, 58)];
    iv.backgroundColor = [UIColor clearColor];
    iv.opaque = NO;
    iv.contentMode = UIViewContentModeScaleAspectFill;
    [iv setImageWithURL:[NSURL URLWithString:URL]];
    cell.backgroundView = iv;

    return cell;
}
4

1 に答える 1

4

主な問題は、セルをデキューしている間に、コレクション ビュー内のすべてのアイテムに対して新しいイメージ ビューを割り当てて初期化していることです。

代わりに、独自の画像セル クラスを作成する必要があります。

@interface CollectionViewImageCell : UICollectionViewCell
@property (nonatomic) UIImageView *imageView;
@end

@implementation CollectionViewImageCell

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setupImageView];
    }
    return self;
}

#pragma mark - Create Subviews

- (void)setupImageView {
    self.imageView = [[UIImageView alloc] initWithFrame:self.bounds];
    self.imageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    // Configure the image view here
    [self addSubview:self.imageView];
}

@end

次に、このクラスを登録すると、cellForItemAtIndexPath:メソッドは次のようになります。

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    CollectionViewImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"thumbCell" forIndexPath:indexPath];

    NSString *URL = [self.album.imageURLs objectAtIndex:(indexPath.item+1)];
    [cell.imageView setImageWithURL:[NSURL URLWithString:URL]];
    return cell;
}

私は SDWebImage にあまり詳しくありません。これにはAFNetworkingを使用する傾向がありますが、画像キャッシュが設定されていることも確認します。

それでもメモリ関連のクラッシュやスクロール パフォーマンスの問題が発生する場合は、おそらく画像の読み込み戦略を改善し、サーバー側で画像を再スケーリング/再サンプリングする必要があります。

于 2013-04-01T11:36:27.973 に答える