UIImageView 中 UIImage 的大小

3

我有一些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.sizeself.image.size ->>> 原始图像大小 imageView.image.size ->>> imageView的大小,而不是所显示图像的大小。
我的问题是关于根据imageView的大小和它的contentmode来确定显示大小。

1
https://dev59.com/t2w15IYBdhLWcg3wD3dk - iPatel
4个回答

5
这里有一个关于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

4
在Swift中,与@jacob-relkin相同。
extension UIImageView {

    func displayedImageBounds() -> CGRect {

        let boundsWidth = bounds.size.width
        let boundsHeight = bounds.size.height
        let imageSize = image!.size
        let imageRatio = imageSize.width / imageSize.height
        let viewRatio = boundsWidth / boundsHeight
        if ( viewRatio > imageRatio ) {
            let scale = boundsHeight / imageSize.height
            let width = scale * imageSize.width
            let topLeftX = (boundsWidth - width) * 0.5
            return CGRectMake(topLeftX, 0, width, boundsHeight)
        }
        let scale = boundsWidth / imageSize.width
        let height = scale * imageSize.height
        let topLeftY = (boundsHeight - height) * 0.5
        return CGRectMake(0,topLeftY, boundsWidth,height)
    }
}

0

这是不可能的。你可以通过以下方法获取原始图像大小

UIImage *image = imageView.image;
CGSize size = image.size;

大小

考虑方向的图像尺寸。(只读)

@property(nonatomic, readonly) CGSize size

0

你也可以使用以下方法获取它:

-(CGSize)imageSizeAfterAspectFit:(UIImageView*)imgview{


float newwidth;
float newheight;

UIImage *image=imgview.image;

if (image.size.height>=image.size.width){
    newheight=imgview.frame.size.height;
    newwidth=(image.size.width/image.size.height)*newheight;

    if(newwidth>imgview.frame.size.width){
        float diff=imgview.frame.size.width-newwidth;
        newheight=newheight+diff/newheight*newheight;
        newwidth=imgview.frame.size.width;
    }

}
else{
    newwidth=imgview.frame.size.width;
    newheight=(image.size.height/image.size.width)*newwidth;

    if(newheight>imgview.frame.size.height){
        float diff=imgview.frame.size.height-newheight;
        newwidth=newwidth+diff/newwidth*newwidth;
        newheight=imgview.frame.size.height;
    }
}

NSLog(@"image after aspect fit: width=%f height=%f",newwidth,newheight);

return CGSizeMake(newwidth, newheight);

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接