arrays - 如何使用ARKit框架获取随机图像?

标签 arrays swift xcode augmented-reality arkit

func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? {

    // Create and configure a node for the anchor added to the view's session.
    let arrayOfImages = ["Photo1", "Photo2", "Photo3", "Photo4", "Photo5", 
                         "Photo6", "Photo7", "Photo8", "Photo9", "Photo10", 
                         "Photo11", "Photo12", "Photo13", "Photo14", "Photo15", 
                         "Photo16", "Photo17", "Photo18", "Photo19", "Photo20", 
                         "Photo21", "Photo22", "Photo23", "Photo24", "Photo25", 
                         "Photo26", "Photo27", "Photo28", "Photo29", "Photo30"]
    
    let node = SKSpriteNode(imageNamed: (arc4random() % UInt32 (arrayOfImages.count)) )
    return node;
}

但它说有一个错误:

错误:

//Can't convert value of type `UIImage` to expected argument type string.

最佳答案

试试这个代码:

let arrayOfImages = ["Photo1", "Photo2", "Photo3", "Photo4", 
                     "Photo5", "Photo6", "Photo7", "Photo8", 
                     "Photo9", "Photo10", "Photo11", "Photo12", 
                     "Photo13", "Photo14", "Photo15", "Photo16", 
                     "Photo17", "Photo18", "Photo19", "Photo20", 
                     "Photo21", "Photo22", "Photo23", "Photo24", 
                     "Photo25", "Photo26", "Photo27", "Photo28", 
                     "Photo29", "Photo30"]

let randomIndex = Int(arc4random_uniform(UInt32(arrayOfImages.count)))
let Image = UIImage(named: arrayOfImages[randomIndex])
let Texture = SKTexture(image: Image)
let Sprite = SKSpriteNode(texture:Texture)

关于arrays - 如何使用ARKit框架获取随机图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46703522/

相关文章:

具有可变维度的 JavaScript 函数数组

swift - 当之前在设备上授权 healthkit 访问时工作正常,但否则会崩溃

swift - 无法将类型 'XCUIElement' 的值转换为预期的参数类型 'SignUpSetUp'

ios - Swift Make Animation 看起来就像去新的 ViewController 而没有 One

ios - 我需要 iOS 设备才能访问 'Archive' Xcode 项目吗?

java - 在字符串中移动字符

javascript - 将提示输入保存到数组中并从中读取

php - 为 PHP 数组中的每个对象添加属性

ios - NSFetchedResultsController 和数组。插入

swift - storageRef.downloadURL 没有在里面执行代码