2011-03-09 27 views

回答

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

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

可能是什么单位?像素还是点数? – byJeevan 2016-01-06 06:39:18

+1

分。乘以scale属性来获取像素。 – rmooney 2016-06-28 14:50:12

+0

这很奇怪。我用相机拍了一张照片,并假定它的高度>宽度。但宽度点是1280和高度720.他们有什么问题? – Henry 2016-10-31 00:28:37

30

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

+3

+1链接至文档... – 2011-03-09 17:30:26

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) ; 
+0

您不必创建另一个对象“UIImageView”,请参阅答案.... – Satyam 2011-12-15 10:03:34

2

以上所述的部分anwsers的,旋转设备时不能很好工作。

尝试:

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

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

let imageViewHeight: CGFloat = imageView.frame.height 

let imageViewWidth: CGFloat = imageView.frame.width