How to get a random image using ARKit framework?
How to get a random image using ARKit framework?
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;
But it says an error saying it:
//Can't convert value of type `UIImage` to expected argument type string.
1 Answer
1
Try this code:
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)
Sorry, I forgot to add the line which takes index and get array element. now try a new one.
– Zeeshan Arif
Oct 12 '17 at 15:35
Thanks for that the array is working now, but I have another problem. In the same phrase when I return the node it says "Error Expected declaration." This is the code I would be very grateful if you could help me out. Thanks a lot Ben "func view(_ view: ARSKView, nodeFor anchor: ARAnchor) -> SKNode? \Code ;return node; ERROR Expected declaration"
– Ben Chevassut
Oct 12 '17 at 18:54
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thanks but this didn't work it now says, Cannot convert value of type UInt32 to expect argument type string. If you have any other suggestions I would be happy to hear them
– Ben Chevassut
Oct 12 '17 at 15:13