iPhone:在AspectFt之后获取图像的大小 [英] iPhone : Getting the size of an image after AspectFt

查看:100
本文介绍了iPhone:在AspectFt之后获取图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

真奇怪的人会被困住,但奇怪的是我。

Real odd one to get stuck on but weirdly I am.

你有一个 imageView 包含一个图片。您将 imageView 缩小,然后告诉它使用 UIViewContentModeScaleAspectFit 。所以你的 imageView 可能是300乘200,但你的缩放图像可能是300乘118或228乘200,因为它的 aspectfit

You you have a imageView containing a image. You size that imageView down and then tell it to use UIViewContentModeScaleAspectFit. so your imageView might be 300 by 200 but your scaled image within could be 300 by 118 or 228 by 200 because its aspectfit.

如何获得实际图像的大小?

imageView.image.size 是原始图片的大小。

imageview.frame imageview的框架不包含图片。

imageview.contentstretch 无效

How on earth do you get the size of the actual image?
imageView.image.size is the size of the original image.
imageview.frame is the frame of the imageview not the contained image.
imageview.contentstretch does not work either

推荐答案

我在UIImageView上编写了一个快速类别来实现:

I have written a quick category on UIImageView to achieve that:

(。h)

@interface UIImageView (additions)
- (CGSize)imageScale;
@end

(。m)

@implementation UIImageView (additions)
- (CGSize)imageScale {
    CGFloat sx = self.frame.size.width / self.image.size.width;
    CGFloat sy = self.frame.size.height / self.image.size.height;
    CGFloat s = 1.0;
    switch (self.contentMode) {
        case UIViewContentModeScaleAspectFit:
            s = fminf(sx, sy);
            return CGSizeMake(s, s);
            break;

        case UIViewContentModeScaleAspectFill:
            s = fmaxf(sx, sy);
            return CGSizeMake(s, s);
            break;

        case UIViewContentModeScaleToFill:
            return CGSizeMake(sx, sy);

        default:
            return CGSizeMake(s, s);
    }
}
@end

乘以原始图像尺寸按给定比例,您将获得实际显示的图像尺寸。

Multiply the original image size by the given scale, and you'll get your actual displayed image size.

这篇关于iPhone:在AspectFt之后获取图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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