iPhone - 我怎样才能获得uiimage的高度和宽度

时间:2011-03-09 17:24:11

标签: ios uiimage cgsize

来自网址Image in Mail

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

如何获得UIImage的高度和宽度?

8 个答案:

答案 0 :(得分:396)

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

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

答案 1 :(得分:33)

使用UIImage实例上的size属性。有关详细信息,请参阅the documentation

答案 2 :(得分:10)

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

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

答案 3 :(得分:5)

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

尝试:

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

答案 4 :(得分: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) ;

答案 5 :(得分:0)

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

let imageViewHeight: CGFloat = imageView.frame.height

let imageViewWidth: CGFloat = imageView.frame.width

答案 6 :(得分:0)

    import func AVFoundation.AVMakeRect

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

    x = imageRect.minX

    y = imageRect.minY

答案 7 :(得分:0)

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

extension UIImage {

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

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