简体   繁体   English

iOS:管理通用应用

[英]iOS: manage an universal app

I created an app for iPad, so I have two different version of image, example: 我为iPad创建了一个应用程序,因此有两个不同版本的图像,例如:

image.png (for iPad 2 or iPad Mini-not retina)
image@2x.png (for iPad >= 3 and iPad mini-retina)

It' very easy. 很简单

Now I should to create an universal version of my app, so I created my new launch image, icon and some background for iphone size without @2x because I don't want consider iphone 3gs, so I don't set image for iphone with @2x. 现在,我应该创建一个通用版本的应用程序,因此我创建了新的启动图像,图标和一些不带@ 2x的iphone尺寸的背景,因为我不想考虑iphone 3gs,所以我不设置带有iphone的图像。 @ 2x。 And it's all ok. 没关系。

But my app is a game, so I have a lot of images that are fine also for my iphone version. 但是我的应用程序是游戏,因此我有很多图像也适合我的iPhone版本。 The problem is that iphone 4, 4s, 5 and 5s take image with @2x, but for these device, especially for iphone 4 these image are very big, and I have some memory leak with crashes. 问题是iphone 4、4s,5和5s用@ 2x拍摄图像,但是对于这些设备,尤其是对于iPhone 4,这些图像非常大,并且由于崩溃而导致内存泄漏。 Now, my question is: is there a way, if device is retina, to take image that don't have the "@2x"?? 现在,我的问题是:如果设备是视网膜,有没有办法获取没有“ @ 2x”的图像?

Sure, just manage opening the images yourself. 当然,您只需要自己打开图像即可。

For instance, I wanted to have a method like "imageNamed" but did not want the caching that imageNamed does, so I wrote my own. 例如,我想拥有一个类似“ imageNamed”的方法,但又不想像imageNamed那样进行缓存,因此我编写了自己的缓存。 This isn't the function you want, but it illustrates the concept and you can make up your own rules. 这不是您想要的功能,但是它说明了概念,您可以制定自己的规则。 It isn't a huge amount of work to do your own implementation. 自己执行实现不是很多工作。 If you wanted the caching of imageNamed, just use imageNamed to open the image that you eventually decide to use. 如果缓存imageNamed,只需使用imageNamed打开最终决定使用的图像。

// An "uncached" version of -imageNamed.
// It looks for the scale-and-idiom suffixes @2x, ~ipad, and @2x~ipad
// and preferentially loads the specific version if it is available.
+ (UIImage *)imageNamedUncached:(NSString *)name {
    NSString *tryName;
    NSString *nonRetinaiPadTryName = @"";
    NSArray *parts = [name componentsSeparatedByString:@"."];
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        if ([[UIScreen mainScreen] scale] == 1.0) {
            // non-retina
            tryName = [parts[0] stringByAppendingString:@"~ipad"];
            tryName = [tryName stringByAppendingString:@"."];
            tryName = [tryName stringByAppendingString:parts[1]];
        } else {
            // retina
            tryName = [parts[0] stringByAppendingString:@"@2x~ipad"];
            tryName = [tryName stringByAppendingString:@"."];
            tryName = [tryName stringByAppendingString:parts[1]];
            nonRetinaiPadTryName = [parts[0] stringByAppendingString:@"~ipad"];
            nonRetinaiPadTryName = [nonRetinaiPadTryName stringByAppendingString:@"."];
            nonRetinaiPadTryName = [nonRetinaiPadTryName stringByAppendingString:parts[1]];
        }
    } else {
        // iPhone
        if ([[UIScreen mainScreen] scale] == 1.0) {
            // non-retina
            tryName = name;
        } else {
            // retina
            tryName = [parts[0] stringByAppendingString:@"@2x"];
            tryName = [tryName stringByAppendingString:@"."];
            tryName = [tryName stringByAppendingString:parts[1]];        }
    }
    NSString *path = [[NSBundle mainBundle] pathForResource:tryName ofType:nil];
    //NSLog(@"For tryName %@ I get path %@",tryName, path);
    if ( ! path) {
        // If this is a retina iPad we made a non-retina try name -- so check that
        if ( ! [nonRetinaiPadTryName isEqualToString: @""]) {
            path = [[NSBundle mainBundle] pathForResource:nonRetinaiPadTryName ofType:nil];
            //NSLog(@"For non-retina ipad try name %@ I get path %@",nonRetinaiPadTryName, path);
        }
        if ( ! path) {
        // Try the unmodified name
        path = [[NSBundle mainBundle] pathForResource:name ofType:nil];
            //NSLog(@"For base name %@ I get path %@",name, path);
        }
    }
    UIImage *pathImage = [UIImage imageWithContentsOfFile:path];
    return pathImage;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM