在 Xcode 14 中对数组使用 Image Literal?

问题描述 投票:0回答:3

我正在关注一个教程,在教程上,当他们单击红色圆圈的图标时,会弹出图像以供选择。但是,当我双击第二个图标时,没有任何反应。

image

我使用了#imageLiteral(),并且第一个图标图像弹出,但不适用于第一个图像之后的任何图像。这是 Xcode 版本 14。

arrays swift xcode image
3个回答
0
投票
   //To set image literals into the array you can use the below variable array.
    var imageLiteralArray = [
        UIImage(imageLiteralResourceName: "card1"),
        UIImage(imageLiteralResourceName: "card2")
    ]
    //to assign array to image view you can use the below code 
    imageView.image = imageLiteralArray[1]

0
投票

您可以使用 imageLiteral() 执行相同的操作,但在逗号(,) 后面留一个空格,然后尝试它会起作用。

@IBAction func rollButtonPressed(_ 发送者: UIButton) {

    [#imageLiteral(resourceName: "DiceOne"), #imageLiteral(resourceName: "DiceTwo"), #imageLiteral(resourceName: "DiceThree")
}

0
投票

正如您所提到的,双击占位符后会显示一些图像。未解析的图像很可能在其名称中带有比例后缀,如下所示:

#imageLiteral("[email protected]")

一旦去掉后缀——即“@2x”部分 - 内联图像预览将显示:

#imageLiteral("img.png")

有关修复此问题的一个提示:即使删除一些字母,Xcode 也不会将内联预览转换为文本,因此请使用“@2x”进行搜索并将其替换为空字符串。

© www.soinside.com 2019 - 2024. All rights reserved.