Tom Xue Tom Xue - 8 days ago 5x
iOS Question

SpriteKit - Background Image is stretched terribly

I set my spriteKit game mode to be:

scene.scaleMode = .aspectFill

And I set my background image.

sky = SKSpriteNode(imageNamed: "Sky")
sky.position = CGPoint(x: 0, y: 0)
sky.size.width = self.size.width
sky.size.height = self.size.height
sky.anchorPoint = CGPoint(x: 0.5, y: 0.5)
sky.zPosition = 1

And the image size is 2732x2048. It looks terrible on 4.7 inch iPhone.
The whole thing is completed stretched.
Is there a way, to crop the image but not stretching it to fit it on iPhone (Same aspect radio for image).

I tried other scale modes, they don't work.
Can somebody tell me what's the right action to take now?

What if I provide iPhone artwork? How do I differentiate from code, like to specify which artwork iPhone should use, and which artwork iPad should use.

Nik Nik

Remove the lines where you set the size of the sky and you should accomplish the following tasks:

Don't set the size as the frame. Instead, set it to the size of the image, so it gets cropped rather than scaled:

sky.size = sky.texture.size

This code would be a replacement for the 2 lines you have that set its width and height.

This only works if your image size fits properly with its default size (size you made it in)

Alternatively, you could make your app universal like so. Take a look at this: iOS Universal Device App with SpriteKit, how to scale nodes for all views?. Then you simply make your background 1024x1024 (or any size big enough to fit all device sizes aspect ratio). This will crop part of it on devices that aren't the same size, but your aspect ratio won't change or scale for different devices.