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 之后获取图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:iPhone:在 AspectFt 之后获取图像的大小
基础教程推荐
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
