我怎么能得到一个uiimage的高度和宽度?

从URL 邮寄图片

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

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

188414 次浏览

在UIImage实例上使用size属性。详见的文档

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


let widthInPoints = image.size.width
let widthInPixels = widthInPoints * image.scale
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) ;

上面的一些答案,转动装置时工作不好。

试一试:

CGRect imageContent = self.myUIImage.bounds;
CGFloat imageWidth = imageContent.size.width;
CGFloat imageHeight = imageContent.size.height;
UIImage *img = [UIImage imageNamed:@"logo.png"];


CGFloat width = img.size.width;
CGFloat height = img.size.height;
let imageView: UIImageView = //this is your existing imageView


let imageViewHeight: CGFloat = imageView.frame.height


let imageViewWidth: CGFloat = imageView.frame.width
    import func AVFoundation.AVMakeRect


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


x = imageRect.minX


y = imageRect.minY

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

extension UIImage {


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


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