[英]How to read localized image with imageWithContentsOfFile
this is my first question here :-) 这是我的第一个问题:-)
I have an image icon_new.png which is localized for 3 languages. 我有一个图像icon_new.png,本地化为3种语言。 I need to load it in run time using something like this:
我需要在运行时加载它使用这样的东西:
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"icon_new.png"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
Problem with above is that it works for non-localized images, but for localized ones, their real path is actually appended with en.lproj, zh-Hans.lproj or other folder names depending on languages included in localization. 上面的问题是它适用于非本地化的图像,但对于本地化的图像,它们的真实路径实际上附加了en.lproj,zh-Hans.lproj或其他文件夹名称,具体取决于本地化中包含的语言。
Now I could check what's current locale and append path accordingly: 现在我可以检查当前的语言环境,并相应地追加路径:
// This path would work for image that's localized - for English version of it.
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"en.lproj/icon_new.png"];
However I wonder if there's automatic way to do this? 不过我想知道是否有自动方式来做到这一点? Just like app loads entire UI .xib file from proper locale path, so basically I would just specify image name rather than it's full path, and get proper localized version.
就像app从正确的语言环境路径加载整个UI .xib文件一样,基本上我只是指定图像名称而不是它的完整路径,并获得适当的本地化版本。
Thanks in advance for your help! 在此先感谢您的帮助!
使用NSBundle
的-pathForResource:ofType:
,它首先搜索主目录,如果找不到那里的资源,则检查相应的本地化目录。
NSString *path = [[NSBundle mainBundle] pathForResource:@"icon_new" ofType:@"png"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.