UIImage size in UIImageView

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

I have some UIImage and display it in UIImageView.

I need to use UIViewContentModeScaleAspectFit content mode.

Here i

4条回答
  •  -上瘾入骨i
    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);
    
    }
    

提交回复
热议问题