简体   繁体   中英

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. 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.

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.

Thanks in advance for your help!

使用NSBundle-pathForResource:ofType: ,它首先搜索主目录,如果找不到那里的资源,则检查相应的本地化目录。

NSString *path = [[NSBundle mainBundle] pathForResource:@"icon_new" ofType:@"png"];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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