我有一些 UIImage
并将其显示在 UIImageView
中。
我需要使用 UIViewContentModeScaleAspectFit
内容模式。
下面是简单的代码:
UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = self.image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
[imageView release];
如何知道屏幕上显示的图像的大小?是否有任何解决方案、属性或我应该手动计算吗?
编辑:
我知道属性 image.size
。
self.image.size
->>> 原图大小
imageView.image.size
->>> imageView 的大小,但不是显示的图像。
我根据 imageView 的大小
询问显示大小,它是 contentmode
。
最佳答案
这是 UIImageView
上的一个类别,您可以使用它根据 ImageView 上设置的 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
关于iphone - UIImageView 中的 UIImage 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15447814/