在iPhone5和iPhone4的xib中处理不同尺寸的图像? [英] Dealing with different size images in a xib for iPhone5 versus iPhone4?

查看:90
本文介绍了在iPhone5和iPhone4的xib中处理不同尺寸的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前关于迁移到iPhone 5的帖子只提到添加新尺寸的启动图像*
,如果需要,可以使用AutoLayout。

The previous posting on here about migrating to iPhone 5 only mention adding a new sized launch image* and maybe using AutoLayout if need be.

但是我有几个xibs,其中有一个背景图像填满了整个屏幕(可能是导航和标签栏,取决于视图)。为了支持iPhone 5,我现在需要针对不同屏幕尺寸的不同图像,如何在xib或其他地方处理?

But I have a few xibs where there is a background image which fills up the whole of the screen (maybe minue the navigation and tab bar, depending upon the view). To support iPhone 5 I will now need different images for the different screen size, how is this dealt with in the xib or elsewhere?

(顺便说一下,如果该应用程序不使用启动图像?)

(*incidentally what do you do if the app doesn't use a launch image?)

推荐答案

这是一个真正可以帮助你的链接。保存启动图像,如果您在文件名末尾添加-568h@2x.png,iOS不会自动选择正确的图像。 链接上方将使您的工作变得轻松。

Here's a link that will actually help you. Save the launch image, iOS doesn't automatically pick the right image if you put in a "-568h@2x.png" at the end of the file name. There are a couple of helper methods mentioned in the above link that will make your job easy.

我已采用上面提到的链接中的代码,这里是Obj-C的辅助函数:

I have adopted the code in the links I mentioned above and here's the helper function for Obj-C:

-(BOOL) IsTall
{
    return ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) && ([[UIScreen mainScreen] bounds].size.height * [[UIScreen mainScreen] scale] >= 1136);
}

一点点类别写作也会有所帮助。

A little category-writing would help too.

@interface NSString (Special)
-(NSString*)correctImageNameForiPhone5
@end

@implementation NSString (Special)
-(NSString*)correctImageNameForiPhone5
{
    if([self isTall])
            return -imageNameAppendedWith_-568h@2x.png_-; //Do the correct NSString magic here
    else
            return -originalImageName-;
}
@end

最后,当您访问UIImage对象时:

Finally, when you are accessing the UIImage object:

NSString *filename = @"backgroundImage.png";
UIImage *img = [UIImage imageNamed:[filename correctImageNameForiPhone5]];

这里的假设是你所有的iPhone5特定图像文件名都以-568h结尾@ 2X。如果你把它放到你的项目中,这个代码示例绝对不会起作用,但你明白了。它需要一些NSString修复。

The assumption here is that you would have all your iPhone5-specific image file names ending with "-568h@2x". This code sample is definitely not gonna work if you just drop it into your project, but you get the idea. It needs some NSString fixes.

这篇关于在iPhone5和iPhone4的xib中处理不同尺寸的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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