2011-03-09 92 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