UIViewContentModeScaleAspectFit の後にサイズ変更された画像の幅を取得する 質問する

UIViewContentModeScaleAspectFit の後にサイズ変更された画像の幅を取得する 質問する

UIImageView があり、次のようにサイズを変更した画像を配置します。

UIImageView *attachmentImageNew = [[UIImageView alloc] initWithFrame:CGRectMake(5.5, 6.5, 245, 134)];
attachmentImageNew.image = actualImage;
attachmentImageNew.backgroundColor = [UIColor redColor];
attachmentImageNew.contentMode = UIViewContentModeScaleAspectFit;

次のようにして、サイズ変更された画像の幅を取得しようとしましたUIImageView

NSLog(@"Size of pic is %f", attachmentImageNew.image.size.width);

しかし、実際には元の画像の幅が返されます。画面に表示される画像のフレームを取得する方法について何かアイデアはありますか?

編集:これが私のUIImageView見た目です。赤い部分はbackgroundColor

ここに画像の説明を入力してください

ベストアンサー1

もっと明確な解決策があるかどうかはわかりませんが、これは機能します:

float widthRatio = imageView.bounds.size.width / imageView.image.size.width;
float heightRatio = imageView.bounds.size.height / imageView.image.size.height;
float scale = MIN(widthRatio, heightRatio);
float imageWidth = scale * imageView.image.size.width;
float imageHeight = scale * imageView.image.size.height;

おすすめ記事