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;