Use @2x retina images for ipad in universal app? and does apple prefer native apps?

我是研究僧i 提交于 2019-12-18 10:22:19

问题


I know there were some discussions about this but i could not find good answer?

My questions are -

  1. I know that -

      [UIImage imageNamed:@"blabla"]
    

will automatically search for the correct image to display (retina or not) on iPhone.

I have a Universal app, and i wish to use the @2x versions on the ipad so i wont have to load an other version of the images (I have hundreds of small images).

is it possible ?

  • I saw in some places that people wrote that apple discourage Universal apps as it prefers building separated apps for iPhone and iPad ? is that correct even when i create a different UI for each?

thanks

shani


回答1:


There is no good built-in way of not duplicating the higher res iphone retina images for the iPad. You could write your own UIImage extension or subclass that uses the user interface idiom macro to determine your platform, then automatically append "@2x" to the image name:

+ (UIImage *) imageNamedSmart:(NSString *)name
{
    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
        return [UIImage imageNamed:[NSString stringWithFormat:@"%@@2x.png", name]];
    return [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", name]];
}

and you'd call it like this:

[UIImage imageNamedSmart:@"myImage"]



回答2:


If you're loading an image named "image" the search paths are likely to be the same as they've always been:

iPhone:

  • 1) image@2x~iphone.png (retina only)
  • 2) image@2x.png (retina only)
  • 3) image~iphone.png
  • 4) image.png

iPad:

  • 1) image@2x~ipad.png (retina only)
  • 2) image@2x.png (retina only)
  • 3) image~ipad.png
  • 4) image.png



回答3:


I improved on Bogatyr's answer by checking if the retina image exists. Probably not overly necessary, but I found it useful when testing so I can just create one image file.

+ (UIImage *) imageNamedSmart:(NSString *)name {
    NSFileManager *fileManager = [[[NSFileManager alloc] init] autorelease];
    NSString *retinaFileName = [NSString stringWithFormat:@"%@@2x", name];

    NSString *filePath = [[NSBundle mainBundle] pathForResource:retinaFileName ofType:@"png"];

    if ( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && [fileManager fileExistsAtPath:filePath]) {
        return [UIImage imageNamed:[retinaFileName stringByAppendingString:@".png"]];
    }
    return [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", name]];
}



回答4:


Maybe you can duplicate all images resources image@2x.png to image~ipad.png. Be careful to the case of "~ipad.png". But you have to manually manage stretched image with Cap (stretchableImageWithLeftCapWidth: topCapHeight:).



来源:https://stackoverflow.com/questions/5088945/use-2x-retina-images-for-ipad-in-universal-app-and-does-apple-prefer-native-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!