gpt4 book ai didi

arrays - 通过本地化改变ImageView的图片

转载 作者:行者123 更新时间:2023-11-28 15:11:20 25 4
gpt4 key购买 nike

您好,我有一个 imageView 来显示一些图片。我想为每种语言显示不同的图片。因此,当用户本地化为德语时,他得到的图片与本地化为英语的用户不同。

这是我的代码。如何为每个本地化制作多个 imageArray?

谢谢

override func viewDidLoad() {

super.viewDidLoad()

mainScrollView.frame = view.frame


imageArray = [#imageLiteral(resourceName: "help0"), #imageLiteral(resourceName: "help1"), #imageLiteral(resourceName: "help2"), #imageLiteral(resourceName: "help3"), #imageLiteral(resourceName: "help4")]



for i in 0..<imageArray.count{

let imageView = UIImageView()
imageView.image = imageArray[i]
imageView.contentMode = .scaleAspectFit
let xPosition = self.view.frame.width * CGFloat(i)

imageView.frame = CGRect(x: xPosition , y: 0, width: self.mainScrollView.frame.width, height: self.mainScrollView.frame.height)



mainScrollView.contentSize.width = mainScrollView.frame.width * CGFloat(i + 1)
mainScrollView.addSubview(imageView)

self.view.sendSubview(toBack: mainScrollView)

}

最佳答案

你可以通过使用 NSLocalizedString 来做到这一点,所以你声明你的数组是这样的:

let help0 = UIImage(named: NSLocalizedString("help0", comment: ""))
let help1 = UIImage(named: NSLocalizedString("help1", comment: ""))
let help2 = UIImage(named: NSLocalizedString("help2", comment: ""))
let help3 = UIImage(named: NSLocalizedString("help3", comment: ""))
let help4 = UIImage(named: NSLocalizedString("help4", comment: ""))

let imageArray = [help0, help1, help2, help3, help4]

然后在您的示例 ENGGER 中添加您的本地化语言,并在这些文件中添加您想要使用的图像,就像这样(a guide 如何添加本地化):

Localization for English:
"help0" = "help0Eng";
"help1" = "help1Eng";
etc...

Localization for German:
"help0" = "help0Ger";
"help1" = "help1Ger";
etc...

而你的help0Enghelp0Ger等就是你的Assets.xcassets中图片的名称。所以会发生以下情况:

  1. NSLocalizedString("help0", comment: "") 将本地化到所选语言的正确文件中
  2. 该图像名称将用于 help0

关于arrays - 通过本地化改变ImageView的图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47582471/

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