简体   繁体   English

UIImageView中的UIImage大小

[英]UIImage size in UIImageView

I have some UIImage and display it in UIImageView . 我有一些UIImage并在UIImageView显示它。

I need to use UIViewContentModeScaleAspectFit content mode. 我需要使用UIViewContentModeScaleAspectFit内容模式。

Here is simple code: 这是简单的代码:

UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
imageView.image = self.image;
imageView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imageView];
[imageView release];

How to know the size of an image which is displayed on the screen? 如何知道屏幕上显示的图像大小? Are there any solutions, properties or shall i calculate it manually? 是否有任何解决方案,属性或我是否应手动计算?

EDIT: 编辑:

I know about the property image.size . 我知道属性image.size

self.image.size ->>> Original image size self.image.size - >>>原始图像大小

imageView.image.size ->>> Size of the imageView, but not the displayed image. imageView.image.size - >>> imageView.image.size大小,但不是显示的图像。

I ask about the displayed size depending on the imageView's size and it's contentmode . 我询问显示的大小取决于imageView's size和它的contentmode

Here's a category on UIImageView that you can use to introspect the bounds of the displayed image based on the UIViewContentMode set on the image view: 这是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

In swift, same as @jacob-relkin 在快速,与@ 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)
    }
}

Its not possible. 这是不可能的。 You can get the original Image size via following method 您可以通过以下方法获取原始图像大小

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

size 尺寸

The dimensions of the image, taking orientation into account. 图像的尺寸,考虑方向。 (read-only) (只读)

@property(nonatomic, readonly) CGSize size

you can also get it by using below method: 您也可以使用以下方法获取它:

-(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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM