简体   繁体   English

iPhone 4s-UIImage CGImage图像尺寸

[英]iphone 4s - UIImage CGImage image dimensions

I have a crash which only occurs on 4S (not on 3GS). 我的崩溃仅发生在4S(不是3GS)上。 I am doubting its because of @2x. 我怀疑是因为@ 2x。 Basically I get raw bytes of image and manipulate. 基本上我得到图像的原始字节并进行操作。 Here's the question I have. 这是我的问题。

I load a image as mentioned in the sample code below. 我按照下面的示例代码中所述加载图像。 At the end, uiWidth should be 2000 and cgwidth should be 2000. Correct? 最后,uiWidth应该为2000,cgwidth应该为2000。对吗? (Would it still be true if image is loaded from camera rolls? Or its autoscaling and uiWidth will be 4000?) (如果从相机胶卷加载图像,这仍然是正确的吗?或者它的自动缩放和uiWidth将为4000?)

//test.jpg is 2000x 1500 pixels.
NSString *fileName = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:fileName];

int uiWidth = image.size.width;

CGImageRef cgimg = image.CGImage;

int cgWidth = CGImageGetWidth(cgimg);

Thank you for your help. 谢谢您的帮助。

The size reported by UIImage is in points, not pixels. UIImage报告的sizeUIImage为单位,而不是像素。 You need to take into account the scale property of UIImage . 您需要考虑UIImagescale属性。

In other words, if test.jpg is 1000x1000 then UIImage.size will report 1000x1000. 换句话说,如果test.jpg为1000×1000,然后UIImage.size将报告1000×1000。 If test@2x.png is 2000x2000 then UIImage.size will also report 1000x1000. 如果test@2x.pngtest@2x.png ,则UIImage.size也将报告1000x1000。 But in the 2nd case, UIImage.scale will report 2. 但是在UIImage.scale情况下, UIImage.scale将报告2。

CGImageGetWidth reports its width in pixels, not points. CGImageGetWidth以像素为单位报告其宽度,而不是点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM