How to set an image based on iphone platform in objective-c
I'm moving my application from 3.x to 4.x as I prepare for the app store and found that I need to have 2 copies of all my custom pngs - my question is how can I determine what img to show and when. Example - how do I know to show the Find开发者_StackOverflow.png vs the Find@2x.png
Is it "safe" or "correct" to look for 4.x specific apis or does the iphone have a way to determine what platform you are on at runtime?
Thank you in advance
When you use standard APIs, the phone handles grabbing the @2x version when necessary. For example if you use [UIImage imageNamed:@"Find.png"];
and run on an iPhone 4, it would load Find@2x.png automatically.
[[UIDevice currentDevice] model];
[[UIDevice currentDevice] systemName];
[[UIDevice currentDevice] systemVersion];
Detect retina screen/iPhone 4 in iPhone SDK
If you use the imageNamed:
method of UIImage
the @2x business is handled for you.
Otherwise you can check the scale of the screen as in this question
If your application is running on (I believe) iOS 4.2 or greater and the images are PNG files you can replace you calls of:
[UIImage imageNamed:@"Find.png"];
with simply:
[UIImage imageNamed:@"Find"];
On the later versions of the OS imageNamed
attempts to treat the incoming parameter as a root name to an image instead of the specific image name itself. In such a case it has the intelligence built-in to load the graphic most appropriate for the device.
精彩评论