问题描述
我可能在这里遗漏了一些明显的东西,但我一直无法解决以下问题:
I'm probably missing something obvious here, yet I've been unable to solve the following problem:
我有一个包含普通和正常图像资源的项目视网膜屏幕,如 someimage.png 和 [email protected] ,它们存储在一个单独的包中。当我构建项目时,Xcode自动将它们打包成一个多页tiff( imageName.tiff
),我在finder中检查了它 - 它实际上是两个图像的多重tiff 。但是,出现了一个问题:我很难加载适当的资源。
I have a project with image resources for both normal and retina screens, like someimage.png and [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.
我所做的是:
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;
}
我试过以下 imageName
:
imageName = [[AuxClass resourcesBundle] pathForResource:@"someimage" ofType:@"png"];
在这种情况下,我有 null
for imageName
。
in this case i have null
for imageName
.
imageName = [[AuxClass resourcesBundle] pathForResource:@"someimage" ofType:@"tiff"];
在这种情况下会返回确认图像路径,但只有在我使用 imageWithContentsOfFile 而不是 imageNamed
,它没有采取适当的资源:尽管有屏幕类型,它仍为视网膜加载资源。
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.
如果我省略filetype(就像我在添加 @ 2x
资源之前所做的那样,并且它工作正常,这是我尝试的第一件事并且确定它会起作用)
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)
imageName = [NSString stringWithFormat: @"%@/%@",
@"MyResourcesBundle.bundle"",
@"someimage" ];
没有任何内容被加载。
添加.tiff扩展名与 pathForResource:
具有相同的效果 - 加载视网膜资源,无视非视网膜屏幕的资源。
Adding ".tiff" extension have the same effect as pathForResource:
- resource for retina is loaded, disregarding resource for non-retina screen.
那么我缺少什么?加载图像的正确方法是什么?
So what am I missing? What's the correct way of loading images?
推荐答案
您是否尝试使用以下方式加载图像:
Have you tried to simply load the image using:
UIImage * someImage = [UIImage imageNamed: @"someimage"];
(假设您的项目中有一个名为'someimage'的图像,例如someimage.png)
(assuming your have an image named 'someimage' in you project, for example someimage.png)
代码将自动选择视网膜/非视网膜版本,具体取决于平台。
The code will automatically pick retina/non-retina versions depending on the platform.
如果问题是那样的话创建TIFF,检查:
If the problem is that the TIFF are created, check:
在最新版本的XCode中,转到编辑器菜单,然后选择验证设置,这将删除该图稿组合。
In latest version of XCode, go to the Editor menu, then select "validate settings", which should remove that artwork combination.
这篇关于适用于iOS的图像资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!