iphone - UIImageView 中的 UIImage 大小

标签 iphone ios objective-c uiimage

我有一些 UIImage 并将其显示在 UIImageView 中。

我需要使用 UIViewContentModeScaleAspectFit 内容模式。

下面是简单的代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = self.image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
[imageView release];

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

编辑:

我知道属性 image.size

self.image.size ->>> 原图大小

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

我根据 imageView 的大小 询问显示大小,它是 contentmode

最佳答案

这是 UIImageView 上的一个类别,您可以使用它根据 ImageView 上设置的 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

关于iphone - UIImageView 中的 UIImage 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15447814/

相关文章:

iphone - 控制iOS6中MKMapView的动画速度

iphone - ObjectiveFlickr - 如何在响应之前未知的照片 ID 上调用 getSizes?

ios - iOS 开发是否有强大的 Objective-C 替代方案

ios - 自定义UITabBar项目

ios - iOS 中带有子菜单的 TableView

iphone - 应用程序在某些国家/地区崩溃

jquery - 如何使用 jquery 在 iPhone 上的网站上拖放 div

ios - 如何测试ios6兼容性?

iphone - "' ViewController ' may not respond to ..."当我*已*在@interface 中声明该方法时发出警告

ios - 从 iPhone 日历中删除所有 future 重复发生的事件