Brian Daneshgar
Brian Daneshgar

Reputation: 343

Setting SKSpriteNode from UIImage

I am having trouble setting a SKSpriteNode from a UIImage. I am using a bridge on the UIImage, converting the image to an SKTexture, and then creating a new SKSpriteNode with that SKTexture, but my code keeps crashing with the following alert "Thread 1: EXC_BAD_ACCESS (code=1, address=0x0)"

CGImageRef textureImage =  (__bridge CGImageRef)(theUIImage);
SKTexture *heroTexture = [SKTexture textureWithCGImage:textureImage];
SKSpriteNode *hero = [SKSpriteNode spriteNodeWithTexture:heroTexture size:CGSizeMake(64, 64)];

Upvotes: 1

Views: 665

Answers (2)

Septronic
Septronic

Reputation: 1176

Try creating a texture using the image: [SKTexture textureWithImageNamed:...] and then use this to creat the Sprite: [SKSprite spriteWithTexture:....]. (It could be SpriteFromTexture)

Upvotes: 1

EDUsta
EDUsta

Reputation: 1932

Instead of bridging the UIImage to CGImageRef, you should use:

CGImageRef textureImage = [theUIImage CGImage];

If your image is not nil, it should work.

Also, you could try:

SKTexture *heroTexture = [SKTexture textureWithImage:theUIImage];

A little tip; always check for "nullity".

Upvotes: 1

Related Questions