在UIViewContentModeScaleAspectFit之后获取已调整大小的图像的宽度

Ser*_*yov 25 iphone cocoa-touch objective-c uiimageview ios

我有我的UIImageView,我把一个图像放入其中,我调整大小如下:

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

我尝试UIImageView通过这样做获得调整大小的图片的宽度:

NSLog(@"Size of pic is %f", attachmentImageNew.image.size.width);
Run Code Online (Sandbox Code Playgroud)

但它实际上返回原始图片的宽度.关于如何获得我在屏幕上看到的图片框架的任何想法?

编辑:这是我的UIImageView外观,红色区域是它的backgroundColor

在此输入图像描述

Mik*_*ail 45

我不知道是否有更明确的解决方案,但这有效:

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;
Run Code Online (Sandbox Code Playgroud)