适用于 iOS 的图像资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12244494/
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
Image resources for iOS
提问by Vladimir
I'm probably missing something obvious here, yet I've been unable to solve the following problem:
我可能在这里遗漏了一些明显的东西,但我一直无法解决以下问题:
I have a project with image resources for both normal and retina screens, like someimage.pngand [email protected], which are stored in a separate bundle. When I build the project, Xcode automatically packs them into a single multipage tiff (imageName.tiff
), I've checked it in finder - it is actually multipage tiff with both images. However, here comes a problem: I struggle to load appropriate resource.
我有一个项目,其中包含用于普通屏幕和视网膜屏幕的图像资源,例如someimage.png和[email protected],它们存储在单独的包中。当我构建项目时,Xcode 会自动将它们打包到一个多页 tiff ( imageName.tiff
) 中,我已经在 finder 中检查了它 - 它实际上是带有两个图像的多页 tiff。但是,这里出现了一个问题:我很难加载适当的资源。
What I do is:
我要做的是:
NSString * imageName = ... ;
NSLog(@"imageName: %@", imageName);
UIImage * someImage = [UIImage imageNamed: imageName];
Also I fave auxiliary method, which returns bundle with resources:
我也喜欢辅助方法,它返回资源包:
+(NSBundle *) resourcesBundle
{
NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"MyResourcesBundle" withExtension:@"bundle"]];
return bundle;
}
I've tried following for imageName
:
我试过以下imageName
:
imageName = [[AuxClass resourcesBundle] pathForResource:@"someimage" ofType:@"png"];
in this case i have null
for imageName
.
在这种情况下,我都null
对imageName
。
imageName = [[AuxClass resourcesBundle] pathForResource:@"someimage" ofType:@"tiff"];
in this case actuall image path is returned, however it only works if I use imageWithContentsOfFile
instead of imageNamed
, and it doesn't take appropriate resource: it loads resource for retina despite the type of screen.
在这种情况下,返回了实际的图像路径,但是它仅在我使用imageWithContentsOfFile
而不是时才有效imageNamed
,并且它不占用适当的资源:尽管屏幕类型不同,它仍会为视网膜加载资源。
If I ommit filetype (as I did before adding @2x
resources, and it worked ok, and was the first thing I tried and was sure it would work)
如果我省略文件类型(就像我在添加@2x
资源之前所做的那样,它工作正常,这是我尝试的第一件事,并确定它会工作)
imageName = [NSString stringWithFormat: @"%@/%@",
@"MyResourcesBundle.bundle"",
@"someimage" ];
nothing get's loaded.
什么都没有加载。
Adding ".tiff" extension have the same effect as pathForResource:
- resource for retina is loaded, disregarding resource for non-retina screen.
添加“.tiff”扩展名与pathForResource:
加载视网膜资源的效果相同,不考虑非视网膜屏幕的资源。
So what am I missing? What's the correct way of loading images?
那么我错过了什么?加载图像的正确方法是什么?
回答by Resh32
Have you tried to simply load the image using:
您是否尝试过使用以下方法简单地加载图像:
UIImage * someImage = [UIImage imageNamed: @"someimage"];
(assuming your have an image named 'someimage' in you project, for example someimage.png)
(假设您的项目中有一个名为“someimage”的图像,例如 someimage.png)
The code will automatically pick retina/non-retina versions depending on the platform.
该代码将根据平台自动选择视网膜/非视网膜版本。
If the problem is that the TIFF are created, check:
如果问题是创建了 TIFF,请检查:
In latest version of XCode, go to the Editor menu, then select "validate settings", which should remove that artwork combination.
在最新版本的 XCode 中,转到“编辑器”菜单,然后选择“验证设置”,这将删除该图稿组合。
回答by Wildaker
Multipage TIFFs are only for OSX; they don't workon iOS.
多页 TIFF 仅适用于 OSX;它们不适用于 iOS。
So you need to stop trying to access resources that are, by their very nature, inaccessible, and do things the right way!
因此,您需要停止尝试访问本质上无法访问的资源,并以正确的方式做事!
You need to open the project that generates the external resources bundle, select the target, go to Build Settings and then the Deployment sub-heading, and set "Combine High Resolution Artwork" to No. Then recompile the external resources bundle.
您需要打开生成外部资源包的项目,选择目标,进入Build Settings,然后是Deployment子标题,将“Combine High Resolution Artwork”设置为No,然后重新编译外部资源包。
In your main project you should now be able to read in the PNGs in the normal manner.
在您的主项目中,您现在应该能够以正常方式读取 PNG 文件。
回答by casero
If you are working with bundles than go to
如果您正在使用捆绑包而不是去
-> build settings -> COMBINE_HIDPI_IMAGES to NO,
-> 构建设置 -> COMBINE_HIDPI_IMAGES 为 NO,
and you just need to clean and build!
你只需要清理和建造!
回答by user2587835
-> build settings -> COMBINE_HIDPI_IMAGES to NO
-> 构建设置 -> COMBINE_HIDPI_IMAGES 为 NO
this is the only solution that works and no other change is necessary
这是唯一有效的解决方案,无需其他更改
回答by Natan R.
When you are creating images for your app, let's say you're creating an image called example
.
当您为您的应用程序创建图像时,假设您正在创建一个名为example
.
You should save the following:
您应该保存以下内容:
- example~iphone.png -> this is for non retina iPhone
- [email protected] -> this is for retina iPhone
- example~ipad.png -> this is for non retina iPad
- [email protected] -> this is for retina iPad
- 示例~iphone.png -> 这是用于非视网膜 iPhone
- 示例[email protected] -> 这是用于视网膜 iPhone
- 示例~ipad.png -> 这适用于非视网膜 iPad
- 示例[email protected] -> 这是用于视网膜 iPad
So, it doesn't matter from where you call it, you just call it this way, assuming myImageView is a UIImageView object:
所以,从哪里调用它并不重要,你只是这样调用它,假设 myImageView 是一个 UIImageView 对象:
[myImageView setImage:[UIImage imageNamed:@"example"]];
Apple strongly recommends you to use png images.Also, the OS takes care of finding the correct image for the desired device. You don't need to worry about finding the path and all that stuff. +(UIImage*)imageNamed:(NSString*)name
looks for the correct image in your resources bundle - and by the way, to return the bundle with your resources just call [NSBundle mainBundle];
Apple 强烈建议您使用 png 图像。此外,操作系统负责为所需设备找到正确的映像。您无需担心找到路径和所有这些东西。+(UIImage*)imageNamed:(NSString*)name
在您的资源包中寻找正确的图像 - 顺便说一下,要返回包含您的资源的包,只需调用[NSBundle mainBundle];