iPhone : Getting the size of an image after AspectFt(iPhone:在 AspectFt 之后获取图像的大小)
问题描述
卡住真的很奇怪,但我很奇怪.
Real odd one to get stuck on but weirdly I am.
你有一个包含图像的 imageView
.你缩小 imageView
的大小,然后告诉它使用 UIViewContentModeScaleAspectFit
.所以您的 imageView
可能是 300 x 200,但您的缩放图像可能是 300 x 118 或 228 x 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 之后获取图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!