如何获取 UIImageView 中缩放后的 UIImage 的大小?

发布于 2024-07-10 00:27:37 字数 319 浏览 8 评论 0原文

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

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

有没有什么方法可以做到这一点,而无需根据 UIImageView 大小和大小自己弄清楚。 UIImage 原始大小(基本上是对其缩放进行逆向工程)?

The image.size attribute of UIImageView gives the size of the original UIImage. I would like to find out the size of the autoscaled image when it is put in the UIImageView (typically smaller than the original).

For example, I have the image set to Aspect Fit. Now I want to know its new height and width on the screen so I can draw accurately on the new scaled image.

Is there any way to do this without figuring it out myself based on the UIImageView size & UIImage original size (basically reverse engineering its scaling)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

偷得浮生 2024-07-17 00:27:37

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

Swift 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)
  }
}

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

Swift 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)
  }
}
凉月流沐 2024-07-17 00:27:37

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

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

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

This simple function will calculate size of image:-

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

For more details, You can refer AVMakeRectWithAspectRatioInsideRect - AVFoundation.

感悟人生的甜 2024-07-17 00:27:37

这个简单的函数将计算长宽比适合后图像的大小:

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


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

}

This simple function will calculate size of image after aspect fit:

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


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

}
半夏半凉 2024-07-17 00:27:37

改编自 cncool 答案,迅速作为 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)
    }
}

An adaptation of cncool answer, in swift as a UIImageView extension, perhaps it could be useful to someone:

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)
    }
}
唠甜嗑 2024-07-17 00:27:37

由于您已将图像视图设置为“宽高比适合”,因此您可以简单地获取图像视图的高度和宽度以及原始图像的高度和宽度,并计算缩放图像的高度和宽度。

也就是说,更好的方法是使用自定义视图而不是 UIImageView。 然后,在自定义视图中自己绘制图像。 这样你就可以控制它的各个方面。

Since you've got the image view set to Aspect Fit, you can simply take the height and width of the image view along with the height and width of the original image and calculate the height and width of the scaled image.

That said, the better way of doing this is to have a custom view instead of a UIImageView. Then, draw the image yourself in the custom view. That way you can control every aspect of it.

不及他 2024-07-17 00:27:37

继@MeetDoshi之后,这里有一个可以适应这一点的扩展。

extension UIImageView {
    /// Retrieve the scaled size of the image within this ImageView.
    /// - Returns: A CGRect representing the size of the image after scaling or nil if no image is set.
    func getScaledImageSize() -> CGRect? {
        if let image = self.image {
            return AVMakeRect(aspectRatio: image.size, insideRect: self.frame);
        }

        return nil;
    }
}

Following up on @MeetDoshi, here is an extension that should accommodate this.

extension UIImageView {
    /// Retrieve the scaled size of the image within this ImageView.
    /// - Returns: A CGRect representing the size of the image after scaling or nil if no image is set.
    func getScaledImageSize() -> CGRect? {
        if let image = self.image {
            return AVMakeRect(aspectRatio: image.size, insideRect: self.frame);
        }

        return nil;
    }
}
骄兵必败 2024-07-17 00:27:37

我编写了一个有用的扩展,将其包含在它的一组附加内容中,可能值得一看。

包含的功能

func getScaledImageSize() -> CGRect?
func getScaledImageSizeWithPadding() -> CGRect?
func getPaddingTop() -> CGFloat? {
func getPaddingBottom() -> CGFloat? {
func getPaddingLeft() -> CGFloat? {
func getPaddingRight() -> CGFloat?
func getScaledCoordinate(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint?
func getScaledCoordinateWithPadding(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint?

https://gist.github.com/nathan-fiscaletti /e1b85f68559f305db7342bfff9574563

I wrote a useful extension that includes this in it's set of additions, might be worth checking out.

Included Functions

func getScaledImageSize() -> CGRect?
func getScaledImageSizeWithPadding() -> CGRect?
func getPaddingTop() -> CGFloat? {
func getPaddingBottom() -> CGFloat? {
func getPaddingLeft() -> CGFloat? {
func getPaddingRight() -> CGFloat?
func getScaledCoordinate(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint?
func getScaledCoordinateWithPadding(forX x: CGFloat, andY y: CGFloat, multiplier:CGFloat = 100.0) -> CGPoint?

https://gist.github.com/nathan-fiscaletti/e1b85f68559f305db7342bfff9574563

妄断弥空 2024-07-17 00:27:37

如果填充框架时保持图像长宽比,则图像视图框架将与图像尺寸不同。

If the frame is filled maintaining the image aspect ratio, the imageview frame will not be the same as the image size.

£烟消云散 2024-07-17 00:27:37

从框架中获取 UIImageView 的大小怎么样? 即imageView.frame?

How about just get the UIImageView size from its frame? i.e. imageView.frame?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文