Detect retina screen/iPhone 4 in iPhone SDK

2020-01-29 04:49发布

In my application I am downloading some images from the web (from my server to be precise), in order to save some bandwith and especially memory on the phone, I provide them in two resolutions: 480x320 for the "old" iPhone series and in 960x640 for the iPhone 4 with the retina display. Now I need to be able to detect from within the app when it is running on a device that supports the retina screen. How could I do that?

I have been thinking about using the code snippet below which would return me a specific device identifier such as eg. "iPhone3", yet then I would be limiting the detection to the iPhone4 and would need to update that code for any subsequent device with a retina display.

size_t size;

// Set 'oldp' parameter to NULL to get the size of the data
// returned so we can allocate appropriate amount of space
sysctlbyname("hw.machine", NULL, &size, NULL, 0); 

// Allocate the space to store name
char *name = malloc(size);

// Get the platform name
sysctlbyname("hw.machine", name, &size, NULL, 0);

// Place name into a string
NSString *machine = [NSString stringWithCString:name];

Is there any better soution (maybe it is very obvious but I missed it)?

11条回答
forever°为你锁心
2楼-- · 2020-01-29 05:17

Go with Robin's answer. One other note: if you do need to check the device name, just use the methods on UIDevice.

[[UIDevice currentDevice] model];
[[UIDevice currentDevice] systemName];
[[UIDevice currentDevice] systemVersion];
查看更多
戒情不戒烟
3楼-- · 2020-01-29 05:18
- (BOOL)isRetina {

    BOOL isRetina = NO;

    if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
        CGFloat scale = [[UIScreen mainScreen] scale];
        if (scale > 1.0) {
            isRetina = YES;
        }
    }

    return isRetina;
}


- (CGSize)sizeInPixels {

    CGRect appFrame = [[UIScreen mainScreen] applicationFrame];
    CGSize screenSize = CGSizeMake(appFrame.size.width, appFrame.size.height);

    return [self isRetina] ? CGSizeMake(screenSize.width * 2, screenSize.height * 2) : screenSize;
}
查看更多
Animai°情兽
4楼-- · 2020-01-29 05:21

If you're using Cocos2d, try the following:

[[CCDirector sharedDirector] winSizeInPixels];

It will return a CGSize with properties width and height.

查看更多
兄弟一词,经得起流年.
5楼-- · 2020-01-29 05:24

Although you've already selected an answer, there is a much easier way when specifically dealing with images so I'll mention it as well.

If you include two images in your bundle of the two sizes (320x480 and 640x960) and you append "@2x" at the end of the latter image's filename, [UIImage imageNamed:] will automagically pick the smaller image for older devices and the 2x for devices with a retina display, provided you leave off the image suffix. Ex.:

2 images named @"image.png" and @"image@2x.png", both included in the app bundle.

Then call:

[UIImage imageNamed:@"image"];

This is also how app icons and the loading screen work.

查看更多
We Are One
6楼-- · 2020-01-29 05:27

I get real screen size (in pixels) by this way:

UIScreen *MainScreen = [UIScreen mainScreen];
UIScreenMode *ScreenMode = [MainScreen currentMode];
CGSize Size = [ScreenMode size]; // <--- Real screen size
查看更多
登录 后发表回答