UIImageView中的UIImage大小

B.S*_*.S. 3 iphone objective-c uiimage ios

我有一些UIImage并显示它UIImageView.

我需要使用UIViewContentModeScaleAspectFit内容模式.

这是简单的代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = self.image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
[imageView release];
Run Code Online (Sandbox Code Playgroud)

如何知道屏幕上显示的图像大小?是否有任何解决方案,属性或我是否应手动计算?

编辑:

我知道这个房产image.size.

self.image.size - >>>原始图像尺寸

imageView.image.size - >>> imageView的大小,但不是显示的图像.

我询问显示的尺寸取决于imageView's size它和它contentmode.

Jac*_*kin 5

这是一个类别UIImageView,您可以使用它来根据UIViewContentMode图像视图上的设置内省显示图像的边界:

@implementation UIImageView (JRAdditions)

- (CGRect)displayedImageBounds {
    UIImage *image = [self image];
    if(self.contentMode != UIViewContentModeScaleAspectFit || !image)
        return CGRectInfinite;

    CGFloat boundsWidth  = [self bounds].size.width,
            boundsHeight = [self bounds].size.height;

    CGSize  imageSize  = [image size];
    CGFloat imageRatio = imageSize.width / imageSize.height;
    CGFloat viewRatio  = boundsWidth / boundsHeight;

    if(imageRatio < viewRatio) {
        CGFloat scale = boundsHeight / imageSize.height;
        CGFloat width = scale * imageSize.width;
        CGFloat topLeftX = (boundsWidth - width) * 0.5;
        return CGRectMake(topLeftX, 0, width, boundsHeight);
    }

    CGFloat scale = boundsWidth / imageSize.width;
    CGFloat height = scale * imageSize.height;
    CGFloat topLeftY = (boundsHeight - height) * 0.5;

    return CGRectMake(0, topLeftY, boundsWidth, height);
}

@end
Run Code Online (Sandbox Code Playgroud)