Objective c 中不同类型屏幕的预处理器指令?

发布于 2024-12-20 07:13:37 字数 689 浏览 4 评论 0 原文

我需要在 iPhone 应用程序中区分视网膜屏幕和普通屏幕,类似于:

#if TARGET_OS_IPHONE_VERSION < 3

NSString *uniquePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"close.png"];
UIImage *image = [UIImage imageWithContentsOfFile: uniquePath];

#endif

#if TARGET_OS_IPHONE_VERSION >= 4

NSString *uniquePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"[email protected]"];
UIImage *image = [UIImage imageWithContentsOfFile: uniquePath];

#endif

有什么想法吗?

I need to differentiate between the retina screens or normal screens in my app for iPhone, similar to this:

#if TARGET_OS_IPHONE_VERSION < 3

NSString *uniquePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"close.png"];
UIImage *image = [UIImage imageWithContentsOfFile: uniquePath];

#endif

#if TARGET_OS_IPHONE_VERSION >= 4

NSString *uniquePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"[email protected]"];
UIImage *image = [UIImage imageWithContentsOfFile: uniquePath];

#endif

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

宁愿没拥抱 2024-12-27 07:13:37

不,你不知道。不是 UIImage,它会为你做到这一点。

No you don't. Not with UIImage, it does that for you.

花心好男孩 2024-12-27 07:13:37

你不能用预处理器来做到这一点。您可以定义自己的符号,但我不确定您会做什么。以某种方式告诉苹果该应用程序的不同版本适用于不同的设备?

更好的方法是在运行时执行此操作。查看 UIScreen scale 属性。

当然,通常情况下你不需要这样做,正如其他答案所说。大多数 UIKit 函数都会为您添加 @2x。您需要了解一些特殊情况,即 scale 属性发挥作用的时候。

You can't do it with the preprocessor. You could define your own symbol but I'm not sure what you'd do then. Somehow tell Apple different versions of the app worked on different devices?

Better is to do it at run time. Look at the UIScreen scale property.

Of course, normally you don't need to do this, as the other answer says. Most UIKit functions will add the @2x for you. There are some corner cases where you need to know, which is when the scale property comes into play.

别念他 2024-12-27 07:13:37

预处理器指令在编译时解析。在您的示例中,这意味着编译器在 iPhone 上运行时不会在这些代码块之间做出决定,它将决定您何时构建应用程序。因此,除非您为每个平台构建不同的应用程序,否则您必须在运行时确定这一点。

我确信有一种方法可以获取您正在运行的 iOS/iPhone 版本。只需在初始化应用程序或需要此代码时执行此操作,然后使用 if/else if 即可。

Preprocessor directives are resolved at compile time. In your example, this means that the compiler will not decide between those blocks of code when it's running on an iphone, it will decide when you build your application. So unless you're building a different application for each platform, you'll have to determine this at runtime.

I'm sure there's a way to get which version of iOS/iPhone you're running on. Just do that when you initialize your app or whenever you need this code, and then use an if/else if there.

乄_柒ぐ汐 2024-12-27 07:13:37

你不需要这个。首先,预处理器无助于定义 Retina 屏幕,因为它们是在编译时定义的。但无论如何,由于苹果惯例,你不需要它。只需在您的项目中包含一个“close.png”就足够了,

NSString *uniquePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"close.png"];
UIImage *image = [UIImage imageWithContentsOfFile: uniquePath];

我想您只是想要这个

UIImage *image = [UIImage imageNamed:@"close.png"];

如果 close.png (以及双倍大小 [email protected]) 在您构建项目时位于您的项目中,第二个样本是要使用的样本。测试一下,你会看到retina手机会显示@2x文件

You don't need this. First of all, preprocessor would not help to define Retina screen as they are defined at compile time. But anyway, you don't need it because of Apple convention. Simply having a "close.png" in your project will be enough

NSString *uniquePath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"close.png"];
UIImage *image = [UIImage imageWithContentsOfFile: uniquePath];

I think you just want this though

UIImage *image = [UIImage imageNamed:@"close.png"];

If close.png (and the double size [email protected]) is in your project when you build it, the second sample is the one to use. Test it, you will see that a retina phone will display the @2x file

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文