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

时间:2017-10-12 07:11:49

标签: 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.

1 个答案:

答案 0 :(得分:0)

试试这段代码:

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)