gpt4 book ai didi

ios - 如何使用 imageWithContentsOfFile 读取本地化图像

转载 作者:搜寻专家 更新时间:2023-10-30 20:19:34 24 4
gpt4 key购买 nike

这是我的第一个问题:-)

我有一个图像 icon_new.png 已本地化为 3 种语言。我需要在运行时使用如下方式加载它:

NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"icon_new.png"];
UIImage *img = [UIImage imageWithContentsOfFile:path];

上面的问题是它适用于非本地化图像,但对于本地化图像,它们的真实路径实际上附加了 en.lproj、zh-Hans.lproj 或其他文件夹名称,具体取决于本地化中包含的语言。

现在我可以检查当前的语言环境并相应地附加路径:

// 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"];

但是我想知道是否有自动的方法来做到这一点?就像应用程序从正确的区域设置路径加载整个 UI .xib 文件一样,所以基本上我只指定图像名称而不是完整路径,并获得正确的本地化版本。

预先感谢您的帮助!

最佳答案

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

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

关于ios - 如何使用 imageWithContentsOfFile 读取本地化图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15845113/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com