したがって、私のTableViewCellにはラベルがあり、ラベルの下にサイズ(320 * 320)のイメージビューがあり、イメージビューの下に別のUIViewがあり、いくつかのものがあります。私のテーブルビューは自動行の高さを使用しており、URLから画像をダウンロードして画像ビューにロードしています。また、画像がダウンロードされた後に画像をキャッシュするために sdwebimage cache を使用しています。
私の問題は現在、横長の画像(画像の幅<画像の高さ)の場合です。AspectFitを使用しても問題なく、画像は画像ビューにうまく収まります。ただし、縦長の画像 (画像の幅 > 画像の高さ) の場合、画像は画像ビューの高さ全体を埋めず (aspectfit を使用しているため)、上下のラベルと UIView の間に空白が残ります。
イメージビューの高さをサイズ変更して、イメージの高さに縮小しようとしました。私がしたことは、画像メソッドのダウンロードの完了ブロックで、画像の幅が大きいかどうかを確認してから、画像ビューを新しいサイズ変更された高さの新しい画像ビューに変更しますが、自動レイアウトで動作させることはできません. 画像が最初に表示されると、画像ビューのサイズが変更されたように見えますが、下にスクロールしてバックアップをスクロールすると、機能しなくなります。
以下は cellForRowAtIndexPath メソッドで使用したコードです。
[[SDImageCache sharedImageCache] queryDiskCacheForKey:cacheKey done:^(UIImage *image, SDImageCacheType cacheType) {
if (image != nil) {
float maxWidth = self.productImageView.frame.size.width;
float maxHeight = self.productImageView.frame.size.height;
float actualWidth = image.size.width;
float actualHeight = image.size.height;
float imageRatio = actualWidth/actualHeight;
if (actualWidth > actualHeight) {
imageRatio = maxWidth/actualWidth;
actualHeight = imageRatio * actualHeight;
if (actualHeight < maxHeight) {
self.productImageView.frame = CGRectMake(self.productImageView.frame.origin.x, self.productImageView.frame.origin.y, maxWidth, actualHeight);
}
}
self.productImageView.contentMode = UIViewContentModeScaleAspectFit;
self.productImageView.image = image;
self.productImageView.clipsToBounds = YES;
} else {
// download image from remote server
[self downloadProductImage:sellPost.productImageURL withCacheKey:cacheKey];
}
}];