Here is what I've found out. Since iOS4,
[[UIScreen mainScreen]applicationFrame].size.width;
and
[[UIScreen mainScreen] applicationFrame].size.height;
give measurements in "points", not "pixels". For everything else,pixels=points, but for the iPhone4, each point has 4 pixels. Normalimages are scaled in the iPhone4, so each pixel in the image ismapped onto a point. This means that the iPhone4 can run iPhoneapps without a noticeable change.
iPhone是以“点”来计量,而不是像素。一般情况,一个点 =一个像素,但是iPhone4一个点等于4个像素,所以用[[UIScreenmainScreen]applicationFrame].size.width取出来的长度和宽度还是320*480.这也意味着iPhone4跑以前的程序基本不用做什么修改。
The "apple" way to add "hi-res" images that take advantage of theiPhone's greater resolution is to replace ".png" with "@2x.png" inthe 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.
但是之前有涉及到图片的,一个像素被拉伸到了4个像素,图片质量会变低。解决方法是替换图片的后缀为"@2x.png",代码中的引用不用做任何改变,系统会根据硬件自动去读入相关文件。
The problem with this is that, if you are trying to develop aUniversal app, and include separate images for all the differentpossible screen resolutions/pixel densities, your app will getbloated pretty quick.
这样解决问题的缺点是,当你有很多图片的时候,你的程序包会有增加得很大块。
A common solution to this problem is to pull all the requiredimages of the 'net. This will make your binary nice and small. Onthe negative side, this will eat into your user's internet quota,and it willreallyannoyusers who don't have wifi--especially if your app has no otherreason to use the 'net (and you don't say your app needs the 'netin your app store description).
对于此问题一般的解决方法是将图片放到网上,运行程序时获取。当时这样也会涉及到用户上网和流量的问题。
Fortunately, I have found another way. Often, whenyouscaledownan image, the iPhone4 is clever enoughto utilise the increased pixel density of the scaled image. Forexample, you might have:
幸运的是,我找到一个另外的办法。当你缩小你的图片时,iPhone4 能聪明地帮你利用好像素,例如:
UIButton *myButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100.0, 50.0)];[indicatorButton setBackgroundImage: [UIImage imageNamed:@"buttonImage.png"] forState:UIControlStateNormal];
Now if buttonImage.png is 200x100, it will be perfectly wellbehaved on everything. Similarly, if you start with a nice 640x960(pixel) image that displays quite nicely on the iPad and you scaleit down to a 320x480 image for smaller screens, using somethinglike:
当buttonImage.png是200x100的时候,图片将完美地显示。同样的,如果你有一个640x960的,在iPad上完美显示的图片,如果你用以下代码缩小它到320x480:
+ (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;}
Itshoulddisplayquite nicely on the iPhone4. The trick is not to double-up on yourscaling. For example, if you do something like:
它在iPhone4也会很好显示。这里的技巧并不在于非得成倍地缩小。看下面的代码:
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];
Then you'll have lost your pixel density and your image will lookall "pixely" on the iPhone4.
通过上面的代码你会得到失真的图片。
Finally, if you want to detect if you are an iPhone4 (not reallynecessary if you use the above technique), the following code maybe useful:
最后,你需要通过代码判断你的设备是否是iPhone4,请参考下面的代码:
+(bool)imAnIphone4
{
return([[UIScreen mainScreen]respondsToSelector:@selector(scale)] && [UIScreen mainScreen].scale==2);
}