简体   繁体   English

图像不适合屏幕

[英]Image doesn't fit to screen

I have an image on screen: 我在屏幕上有一张图片:

    image = SKSpriteNode(imageNamed: "image.png")
    image.name = "leftside1"
    image.position = CGPoint(x: 0, y: 0)
    image.zPosition = 3


    self.addChild(image)

Image has w250xh1920 - when I show screen, image not fit to screen, its bigger up and down, but I have set aspectFit in previous scene: 图像的宽度为w250xh1920-当我显示屏幕时,图像不适合屏幕,其上下尺寸较大,但是我在上一场景中设置了AspectFit:

 nextScene!.scaleMode = .aspectFit

Try aspectFill doesn't solve my problem. 尝试AspectFill无法解决我的问题。 Any advice? 有什么建议吗? Thanks! 谢谢!

Try setting the image size to be the size of the scene like this. 尝试将图像尺寸设置为这样的场景尺寸。

if let size = nextScene?.size {
    image.size = size
}

Here is an example of how you can dynamically calculate the width. 这是如何动态计算宽度的示例。

if let size = nextScene?.size {
    // Calculate ratio
    let ratio = size.width / size.height
    // Multiple image width by ratio
    let width = image.size.width * ratio
    // Create a new CGSize
    let newSize = CGSize(width: width, height: size.height)

    image.size = newSize
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM