当前位置:  开发笔记 > IOS > 正文

如何在UIImageView中获得缩放的UIImage的大小?

如何解决《如何在UIImageView中获得缩放的UIImage的大小?》经验,为你挑选了4个好方法。

image.size属性UIImageView给出了原来的大小UIImage.我想知道放入的自动缩放图像的大小UIImageView(通常小于原始图像).

例如,我将图像设置为Aspect Fit.现在我想知道它在屏幕上的新高度和宽度,以便我可以准确地在新的缩放图像上绘制.

有没有办法做到这一点,而不是根据UIImageView大小和UIImage原始大小自己搞清楚(基本上是逆向工程缩放)?



1> 小智..:

Objective-C的:

-(CGRect)frameForImage:(UIImage*)image inImageViewAspectFit:(UIImageView*)imageView
{
    float imageRatio = image.size.width / image.size.height;
    float viewRatio = imageView.frame.size.width / imageView.frame.size.height;
    if(imageRatio < viewRatio)
    {
        float scale = imageView.frame.size.height / image.size.height;
        float width = scale * image.size.width;
        float topLeftX = (imageView.frame.size.width - width) * 0.5;
        return CGRectMake(topLeftX, 0, width, imageView.frame.size.height);
    }
    else
    {
        float scale = imageView.frame.size.width / image.size.width;
        float height = scale * image.size.height;
        float topLeftY = (imageView.frame.size.height - height) * 0.5;

        return CGRectMake(0, topLeftY, imageView.frame.size.width, height);
    }
}

斯威夫特4:

func frame(for image: UIImage, inImageViewAspectFit imageView: UIImageView) -> CGRect {
  let imageRatio = (image.size.width / image.size.height)
  let viewRatio = imageView.frame.size.width / imageView.frame.size.height
  if imageRatio < viewRatio {
    let scale = imageView.frame.size.height / image.size.height
    let width = scale * image.size.width
    let topLeftX = (imageView.frame.size.width - width) * 0.5
    return CGRect(x: topLeftX, y: 0, width: width, height: imageView.frame.size.height)
  } else {
    let scale = imageView.frame.size.width / image.size.width
    let height = scale * image.size.height
    let topLeftY = (imageView.frame.size.height - height) * 0.5
    return CGRect(x: 0.0, y: topLeftY, width: imageView.frame.size.width, height: height)
  }
}



2> Meet Doshi..:

这个简单的函数将计算图像的大小: -

[imageView setFrame:AVMakeRectWithAspectRatioInsideRect(image.size, imageView.frame)];

有关更多详细信息,您可以参考AVMakeRectWithAspectRatioInsideRect - AVFoundation.


这绝对是最好的答案!我不知道这个功能,非常感谢;)

3> Oleh Kudinov..:

这个简单的函数将在方面拟合后计算图像的大小:

-(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: ,newwidth,newheight);


    //adapt UIImageView size to image size
    //imgview.frame=CGRectMake(imgview.frame.origin.x+(imgview.frame.size.width-newwidth)/2,imgview.frame.origin.y+(imgview.frame.size.height-newheight)/2,newwidth,newheight);

    return CGSizeMake(newwidth, newheight);

}



4> Cesar..:

cncool答案的改编,在swift中作为UIImageView扩展,也许它可能对某人有用:

extension UIImageView{
    func frameForImageInImageViewAspectFit() -> CGRect
    {
        if  let img = self.image {
            let imageRatio = img.size.width / img.size.height;

            let viewRatio = self.frame.size.width / self.frame.size.height;

            if(imageRatio < viewRatio)
            {
                let scale = self.frame.size.height / img.size.height;

                let width = scale * img.size.width;

                let topLeftX = (self.frame.size.width - width) * 0.5;

                return CGRectMake(topLeftX, 0, width, self.frame.size.height);
            }
            else
            {
                let scale = self.frame.size.width / img.size.width;

                let height = scale * img.size.height;

                let topLeftY = (self.frame.size.height - height) * 0.5;

                return CGRectMake(0, topLeftY, self.frame.size.width, height);
            }
        }

        return CGRectMake(0, 0, 0, 0)
    }
}

推荐阅读
拾味湖
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有