UIImageView中的UIImage大小 [英] UIImage size in UIImageView

查看:122
本文介绍了UIImageView中的UIImage大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some UIImage and display it in UIImageView.

我需要使用 UIViewContentModeScaleAspectFit 内容模式。

这是简单的代码:

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

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

How to know the size of an image which is displayed on the screen? Are there any solutions, properties or shall i calculate it manually?

编辑:

我知道属性 image.size

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

self.image.size ->>> Original image size

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

imageView.image.size ->>> Size of the imageView, but not the displayed image.

我询问显示的大小取决于 imageView的大小并且它是 contentmode

I ask about the displayed size depending on the imageView's size and it's contentmode.

推荐答案

这是<$ c上的类别$ c> UIImageView 可用于根据图像视图上设置的 UIViewContentMode 内省显示图像的边界:

Here's a category on UIImageView that you can use to introspect the bounds of the displayed image based on the UIViewContentMode set on the image view:

@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

这篇关于UIImageView中的UIImage大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆