9

I've a problem with creating an SKSpriteNode with an UIImage. I'm creating the assets I use for my App with PaintCode (PaintCode creates a Swift file with drawing methods and image methods which allow me to display the created images).

My problem now is that I want to create a simple SKSpriteNode using my PaintCode images but I don't know how to add them to the SpriteNode.

I tried something like this (line 18):

let Bottom = SKSpriteNode(texture: SKTexture(image: StyleKitName.drawBottom(frame: CGRect(x: 50, y: 50, width: 50, height: 50), isWinter: false)))

But it returns following error:

/Users/myMacName/Documents/App Development/AppName/AppName/GlobalFunctions.swift:18:44: Cannot find an initializer for type 'SKTexture' that accepts an argument list of type '(image: ())'

The StyleKitName.drawBottom function returns an UIImage. It would be nice if anyone could tell me how to create a SKSpriteNode with an UIImage.

I appreciate any help. Thanks.

Blackhawk
  • 133
  • 1
  • 6
  • 1
    Where did you use ```let Bottom = SKSpriteNode(texture: SKTexture(image: StyleKitName.drawBottom(frame: CGRect(x: 50, y: 50, width: 50, height: 50), isWinter: false)))``` this line of code? – mert Aug 10 '15 at 15:49
  • This line of code is line 18 and the error appears in the same line. – Blackhawk Aug 10 '15 at 19:14

3 Answers3

19

If you have a UIImage, you can make a sprite node with it likewise:

let Image = //do your setup here to make a UIImage
let Texture = SKTexture(image: Image)
let Sprite = SKSpriteNode(texture:Texture)

Im not sure where or how you called the methods, but I can assure that these are the correct initializers.

MaxKargin
  • 1,560
  • 11
  • 13
  • Thanks this helped me to realize that I called the wrong function ("drawButton" instead of "imageOfButton" which was created by PaintCode). And sorry for wasting your time with such a stupid mistake. – Blackhawk Aug 10 '15 at 19:24
  • glad you figured it out! – MaxKargin Aug 10 '15 at 19:25
  • "small" upgrade to the question - same for extended UIImage with a gif? This is a big problematic as the image will have to be asynced so in this answer `SKTexture` will always get an error.. I know that no one requested it and Im sorry.. still would be nice to get an answer :) – Blue Bot Aug 15 '20 at 13:00
1

if you want connect it from Scene Editor

    let movieImage = self.childNode(withName: "MovieImage") as! SKSpriteNode?
    let Image = UIImage(named: "imageName")
    let Texture = SKTexture(image: Image!)
    movieImage?.texture = Texture
Ahmed Safadi
  • 4,402
  • 37
  • 33
1

If you have an iPad and want to assign UIImage to SKSpriteNode

let myImage = SKSpriteNode(texture:SKTexture(image: UIImage))

Now you can import images from files app on iPad and easily assign images to it.