我有一些UIImage
并将其显示在UIImageView
.
我需要使用UIViewContentModeScaleAspectFit
内容模式。
这是简单的代码:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = self.image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
[imageView release];
如何知道屏幕上显示的图像的大小?有没有任何解决方案、属性或者我应该手动计算它?
EDIT:
我了解该房产image.size
.
self.image.size
->>> 原始图像尺寸
imageView.image.size
->>> imageView 的大小,但不是显示图像的大小。
我询问显示的尺寸取决于imageView's size
这是contentmode
.
这里有一个类别UIImageView
您可以使用它来根据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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)