ios iphone - 我怎样才能得到uiimage的高度和宽度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5249664/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:10:48  来源:igfitidea点击:

iphone - how can i get the height and width of uiimage

iosuiimagecgsize

提问by Satyam

From the URL Image in Mail

来自邮件中的 URL图片

I'm adding image to mail view. It will show full image. But I want to calculate, proportionally change the height and width of the image.

我正在向邮件视图添加图像。它将显示完整图像。但我想计算,按比例改变图像的高度和宽度。

How can I get the height and width of UIImage?

我怎样才能得到 的高度和宽度UIImage

回答by Jgubman

let heightInPoints = image.size.height
let heightInPixels = heightInPoints * image.scale

let widthInPoints = image.size.width
let widthInPixels = widthInPoints * image.scale

回答by Anomie

Use the sizeproperty on the UIImage instance. See the documentationfor more details.

使用sizeUIImage 实例上的属性。有关更多详细信息,请参阅文档

回答by MaxEcho

UIImage *img = [UIImage imageNamed:@"logo.png"];

CGFloat width = img.size.width;
CGFloat height = img.size.height;

回答by Sinuhe Huidobro

Some of the anwsers above, don't work well when rotating device.

上面的一些答案在旋转设备时效果不佳。

Try:

尝试:

CGRect imageContent = self.myUIImage.bounds;
CGFloat imageWidth = imageContent.size.width;
CGFloat imageHeight = imageContent.size.height;

回答by Sayeed S. Alam

UIImageView *imageView = [[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"MyImage.png"]]autorelease];

NSLog(@"Size of my Image => %f, %f ", [[imageView image] size].width, [[imageView image] size].height) ;

回答by ironRoei

    import func AVFoundation.AVMakeRect

    let imageRect = AVMakeRect(aspectRatio: self.image!.size, insideRect: self.bounds)

    x = imageRect.minX

    y = imageRect.minY

回答by Bruno Eigenmann

There are a lot of helpful solutions out there, but there is no simplified way with extension. Here is the code to solve the issue with an extension:

有很多有用的解决方案,但没有扩展的简化方法。这是通过扩展解决问题的代码:

extension UIImage {

    var getWidth: CGFloat {
        get {
            let width = self.size.width
            return width
        }
    }

    var getHeight: CGFloat {
        get {
            let height = self.size.height
            return height
        }
    }
}

回答by Wyatt

let imageView: UIImageView = //this is your existing imageView

let imageViewHeight: CGFloat = imageView.frame.height

let imageViewWidth: CGFloat = imageView.frame.width