UIImage size in UIImageView

前端 未结 4 487
天涯浪人
天涯浪人 2021-01-06 18:40

I have some UIImage and display it in UIImageView.

I need to use UIViewContentModeScaleAspectFit content mode.

Here i

相关标签:
4条回答
  • 2021-01-06 18:46

    In swift, same as @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 讨论(0)
  • 2021-01-06 18:50

    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);
    
    }
    
    0 讨论(0)
  • 2021-01-06 18:53

    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
    
    0 讨论(0)
  • 2021-01-06 19:06

    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:

    @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
    
    0 讨论(0)
提交回复
热议问题