模拟器iPhone Retina屏幕分辨率错误 [英] Simulator iPhone Retina has wrong screen resolution

查看:161
本文介绍了模拟器iPhone Retina屏幕分辨率错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写通用应用程序.对于不同的屏幕分辨率,显示应略有不同.但是当我这样编码时:

I'm trying to write a Universal Application. The display should be slightly different for different screen resolutions. But when I code like this:

- (void)viewDidLoad {
    SCREEN_WIDTH=[[UIScreen mainScreen] applicationFrame].size.width;
    SCREEN_HEIGHT=[[UIScreen mainScreen] applicationFrame].size.height;
    NSLog(@"w:%f h:%f",SCREEN_WIDTH,SCREEN_HEIGHT);
...
}

即使模拟器设置为
,我也会得到输出:w:320.000000 h:480.000000 硬件->设备-> iPhone(视网膜)
此外,具有此分辨率的图像将在模拟器中显示为全屏图像.
我知道我应该得到w:640.000000 h:960.000000.
其他人会这样吗?以及为什么/如何解决的任何想法? 请参阅相关主题:此处

I get output: w:320.000000 h:480.000000 even when the simulator is set to
Hardware->Device->iPhone (Retina)
Furthermore, images with this resolution display as full-screen images in the simulator.
I understand I should be getting w:640.000000 h:960.000000.
Is it like this for anyone else? And any ideas why/how to fix? See the related thread: here

推荐答案

以下是我发现的内容.从iOS4开始,

Here is what I've found out. Since iOS4,

[[UIScreen mainScreen] applicationFrame].size.width;

[[UIScreen mainScreen] applicationFrame].size.height;

给出点"而不是像素"的测量值.对于其他所有像素,像素=点,但是对于iPhone4,每个点都有4像素.普通图像在iPhone4中缩放,因此图像中的每个像素都映射到一个点上.这意味着iPhone4可以运行iPhone应用,而无需进行明显的更改.

give measurements in "points", not "pixels". For everything else, pixels=points, but for the iPhone4, each point has 4 pixels. Normal images are scaled in the iPhone4, so each pixel in the image is mapped onto a point. This means that the iPhone4 can run iPhone apps without a noticeable change.

添加利用iPhone更高分辨率的高分辨率"图像的苹果"方法是将图像文件名中的".png"替换为"@ 2x.png",并将像素密度加倍(有效地,仅是图像中的宽度和高度).重要的是,请勿更改代码中引用图像的方式.
因此,如果您的代码中包含"img.png",iPhone4将加载"img@2x.png"图像(如果有).

The "apple" way to add "hi-res" images that take advantage of the iPhone's greater resolution is to replace ".png" with "@2x.png" in the image file name, and double the pixel density (effectively, just the width&height) in the image. Importantly, don't change the way the image is referred to in your code.
So if you have "img.png" in your code, iPhone4 will load the "img@2x.png" image if it is available.

这样做的问题是,如果您尝试开发通用应用程序,并且针对所有可能的屏幕分辨率/像素密度添加单独的图像,则您的应用程序很快就会肿.

The problem with this is that, if you are trying to develop a Universal app, and include separate images for all the different possible screen resolutions/pixel densities, your app will get bloated pretty quick.

此问题的常见解决方案是提取所有所需的'net图片.这将使您的二进制文件变小.不利的一面是,这会吞噬您用户的互联网配额,并且会真的惹恼那些没有wifi的用户-特别是如果您的应用没有其他理由使用'net(并且您并不是说您的应用需要在应用商店说明中添加"net".

A common solution to this problem is to pull all the required images of the 'net. This will make your binary nice and small. On the negative side, this will eat into your user's internet quota, and it will really annoy users who don't have wifi--especially if your app has no other reason to use the 'net (and you don't say your app needs the 'net in your app store description).

幸运的是,我找到了另一种方法.通常,当您缩小图像时,iPhone4足够聪明,可以利用缩放后图像的增加的像素密度.例如,您可能有:

Fortunately, I have found another way. Often, when you scale down an image, the iPhone4 is clever enough to utilise the increased pixel density of the scaled image. For example, you might have:

UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100.0, 50.0)];
[indicatorButton setBackgroundImage:
   [UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];

现在,如果buttonImage.png为200x100,则在所有情况下都将表现良好.同样,如果您从640x960(像素)的漂亮图像开始,并在iPad上很好地显示,然后将其缩小到320x480的图像以用于较小的屏幕,例如:

Now if buttonImage.png is 200x100, it will be perfectly well behaved on everything. Similarly, if you start with a nice 640x960 (pixel) image that displays quite nicely on the iPad and you scale it down to a 320x480 image for smaller screens, using something like:

+ (UIImage*)imageWithImage:(UIImage*)image newX:(float)newX newY:(float)newY{
    CGSize newSize=CGSizeMake((CGFloat)newX, (CGFloat)newY);
    UIGraphicsBeginImageContext(newSize);
    [image drawInRect:CGRectMake(0,0,newX,newY)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

它应该在iPhone4上很好地显示.诀窍不是扩大您的缩放比例.例如,如果您执行以下操作:

It should display quite nicely on the iPhone4. The trick is not to double-up on your scaling. For example, if you do something like:

UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100.0, 50.0)];
    [indicatorButton setBackgroundImage:
       [Utilities imageWithImage:[UIImage imageNamed:@"buttonImage.png"] newX:100 newY:50] forState:UIControlStateNormal];

然后,您将失去像素密度,并且图像在iPhone4上将显示为所有像素".

Then you'll have lost your pixel density and your image will look all "pixely" on the iPhone4.

最后,如果要检测自己是否是iPhone4(如果使用上述技术,则不是必须的),以下代码可能会有用:

Finally, if you want to detect if you are an iPhone4 (not really necessary if you use the above technique), the following code may be useful:

+(bool)imAnIphone4{
    return([[UIScreen mainScreen]respondsToSelector:@selector(scale)] && [UIScreen mainScreen].scale==2);
}

这篇关于模拟器iPhone Retina屏幕分辨率错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆