I'm setting an image as the background for a SKScene
with code similar to the following
/* In SKScene subclass */
background = SKSpriteNode()
background.anchorPoint = CGPointMake(0,1)
background.position = CGPointMake(0, size.height)
background.zPosition = Layer.Background
background.size = view!.bounds.size
background.texture = SKTexture(image: <# UIImage #>)
addChild(background)
This works to insert the image as a background for the scene, but if the image isn't the same aspect ratio as the background
node, it is stretched to fill it.
(on the left is the result when an image is cropped to fit the aspect ratio of the SKSpriteNode
, and on the right is the result when the image is of a different aspect ratio)
Is there a way to make the SKSpriteNode respect the original aspect ratio of the image, in the way that a UIImageView
can be set to use UIViewContentModeScaleAspectFill
?
EDIT
Changed mention of UIViewContentModeScaleAspectFit
to UIViewContentModeScaleAspectFill
, got them mixed up.
UIViewContentModeScaleAspectFill
, notFit
, whoops. So I would want the texture to completely fill the scene, while respecting the image's aspect ratio, even if it were to leave out parts of the image to do so. I'll edit my question to reflect my mistake. – Ziewvater