How to get the size of a scaled UIImage in UIImage

2019-01-12 22:28发布

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)?

9条回答
趁早两清
2楼-- · 2019-01-12 22:59

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

查看更多
一夜七次
3楼-- · 2019-01-12 23:03

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;
    }
}
查看更多
祖国的老花朵
4楼-- · 2019-01-12 23:04

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.

查看更多
神经病院院长
5楼-- · 2019-01-12 23:05

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)
    }
}
查看更多
时光不老,我们不散
6楼-- · 2019-01-12 23:09

This simple function will calculate size of image:-

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

For more details, You can refer AVMakeRectWithAspectRatioInsideRect - AVFoundation.

查看更多
三岁会撩人
7楼-- · 2019-01-12 23:09

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

查看更多
登录 后发表回答