xcode iPhone 5 的新图像名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12497776/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
New image name for iPhone 5
提问by mmoore410
With the retina we make images with the @2x in the name. I see where the default image has to be default-568h@2x but this does not seem to be the case for other images. Like if my background is bg.png and [email protected] I tried using [email protected] but that does not work. Can somebody tell me what the images need to be named to support the iPhone 5?
使用视网膜,我们制作名称中带有@2x 的图像。我看到默认图像必须是 default-568h@2x ,但其他图像似乎并非如此。就像我的背景是 bg.png 和 [email protected] 我尝试使用 [email protected] 但这不起作用。有人可以告诉我需要为图像命名以支持 iPhone 5 吗?
采纳答案by Jason Coco
There is no specific image name. Having the Default-568h@2x will launch that image on an iPhone 5 or iPod Touch 5G and will enable the non-letterbox mode. After that, you need to design your views to be flexible. There is no special "image name" or anything for the new size.
没有特定的图像名称。拥有 Default-568h@2x 将在 iPhone 5 或 iPod Touch 5G 上启动该图像,并将启用非信箱模式。之后,您需要将视图设计为灵活的。新尺寸没有特殊的“图像名称”或任何内容。
For your background, for example, you should probably be using an image that is capable of stretching or tiling and have it configured properly before setting it.
例如,对于您的背景,您可能应该使用能够拉伸或平铺的图像,并在设置之前对其进行正确配置。
回答by avishic
No special suffix for iPhone 5 (4'' display), just the specific [email protected] file.
iPhone 5(4 英寸显示屏)没有特殊的后缀,只有特定的 [email protected] 文件。
Here's a macro to handle it:
这是一个处理它的宏:
// iPhone 5 support
#define ASSET_BY_SCREEN_HEIGHT(regular, longScreen) (([[UIScreen mainScreen] bounds].size.height <= 480.0) ? regular : longScreen)
Usage: (assets names - image.png, [email protected], [email protected])
用法:(资产名称 - image.png、[email protected]、[email protected])
myImage = [UIImage imageNamed:ASSET_BY_SCREEN_HEIGHT(@"image",@"image-568h")];
回答by Wasim
iPhone 5 does not have a different pixel density, it's the same retina display PPI as the iPhone 4/4S, it's just a different screen size. The @2x images will be used on iPhone 5 as well as 4/4S.
iPhone 5 没有不同的像素密度,它与 iPhone 4/4S 的视网膜显示 PPI 相同,只是屏幕尺寸不同。@2x 图像将用于 iPhone 5 和 4/4S。
回答by MonsieurDart
To complete Jason's answser, I would propose: What about overriding the UIImage
's imageNamed:
method to have it happen the "-568" suffix to the name of your image? Or add a new method called resolutionAdaptedImageNamed:
to the UIImage
maybe using a category.
为了完成 Jason 的回答,我建议:如何重写UIImage
'simageNamed:
方法让它发生图像名称的“-568”后缀?或者添加一个新方法调用resolutionAdaptedImageNamed:
到UIImage
可能使用类别。
If I have a bit of time in the next days, I will try to post the code for that.
如果接下来几天我有一点时间,我会尝试发布代码。
Caution: will not work for images in the Nib files.
注意:不适用于 Nib 文件中的图像。
回答by hsarret
If you are using Xcode 5, you can use asset catalog (see usage there Apple's documentation)
如果您使用的是 Xcode 5,则可以使用资产目录(请参阅Apple 文档中的用法)
Once your asset catalog is created [ UIImage imagedNamed: @"your_image_set" ]
will pull right image based on device.
创建资产目录后,[ UIImage imagedNamed: @"your_image_set" ]
将根据设备提取正确的图像。
回答by sinh99
You can also make category for this just make category as below .
您也可以为此制作类别,如下所示。
UIImage+Retina4.h
#import <UIKit/UIKit.h>
#import <objc/runtime.h>
@interface UIImage (Retina4)
@end
UIImage+Retina4.m
#import "UIImage+Retina4.h"
static Method origImageNamedMethod = nil;
@implementation UIImage (Retina4)
+ (void)initialize {
origImageNamedMethod = class_getClassMethod(self, @selector(imageNamed:));
method_exchangeImplementations(origImageNamedMethod,
class_getClassMethod(self, @selector(retina4ImageNamed:)));
}
+ (UIImage *)retina4ImageNamed:(NSString *)imageName {
// NSLog(@"Loading image named => %@", imageName);
NSMutableString *imageNameMutable = [imageName mutableCopy];
NSRange retinaAtSymbol = [imageName rangeOfString:@"@"];
if (retinaAtSymbol.location != NSNotFound) {
[imageNameMutable insertString:@"-568h" atIndex:retinaAtSymbol.location];
} else {
CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) {
NSRange dot = [imageName rangeOfString:@"."];
if (dot.location != NSNotFound) {
[imageNameMutable insertString:@"-568h@2x" atIndex:dot.location];
} else {
[imageNameMutable appendString:@"-568h@2x"];
}
}
}
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageNameMutable ofType:@"png"];
if (imagePath) {
return [UIImage retina4ImageNamed:imageNameMutable];
} else {
return [UIImage retina4ImageNamed:imageName];
}
return nil;
}
@end
And you can directly check using import this category as below where you wont to check 568 or normal image
您可以直接使用导入此类别进行检查,如下所示,您不会检查 568 或正常图像
imgvBackground.image=[UIImage imageNamed:@"bkground_bg"];//image name without extantion