247

来自邮件中的 URL图像

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

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

4

8 回答 8

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

let widthInPoints = image.size.width
let widthInPixels = widthInPoints * image.scale
于 2011-03-09T17:28:25.907 回答
36

Use the size property on the UIImage instance. See the documentation for more details.

于 2011-03-09T17:26:25.080 回答
12
UIImage *img = [UIImage imageNamed:@"logo.png"];

CGFloat width = img.size.width;
CGFloat height = img.size.height;
于 2015-06-11T15:07:04.170 回答
6

上面的一些答案,在旋转设备时不能很好地工作。

尝试:

CGRect imageContent = self.myUIImage.bounds;
CGFloat imageWidth = imageContent.size.width;
CGFloat imageHeight = imageContent.size.height;
于 2012-10-31T21:52:56.687 回答
2
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) ;
于 2011-12-15T08:09:39.480 回答
2

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

extension UIImage {

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

    var getHeight: CGFloat {
        get {
            let height = self.size.height
            return height
        }
    }
}
于 2020-04-19T21:31:41.573 回答
1
    import func AVFoundation.AVMakeRect

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

    x = imageRect.minX

    y = imageRect.minY
于 2019-05-23T11:06:06.067 回答
-3
let imageView: UIImageView = //this is your existing imageView

let imageViewHeight: CGFloat = imageView.frame.height

let imageViewWidth: CGFloat = imageView.frame.width
于 2017-03-04T05:57:53.783 回答