在SpriteKit中向场景添加模糊会导致图像大小问题

时间:2017-05-09 10:36:03

标签: swift sprite-kit

我正在尝试使用屏幕截图方法将我背景的模糊图像添加到场景中 - 简要概述here和其他地方。

然而,在我的视网膜屏幕上,覆盖的图像是1024 x 768而不是2048 x 1536.这使得它在屏幕上看起来非常小。如何通过调整此代码来覆盖正确尺寸的图像?

func blurWithCompletion() {
    if let effectNode = scene?.childNode(withName: "effectsNode") as? SKEffectNode {
        let  blur = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius": 10.0]);
        effectNode.filter = blur;
        effectNode.shouldRasterize = true;
        effectNode.shouldEnableEffects = true;

        UIGraphicsBeginImageContextWithOptions((view?.frame.size)!, true, 2.0)
        view?.drawHierarchy(in: (view?.frame)!, afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        let node = SKSpriteNode(texture: SKTexture(image: image!));
        effectNode.addChild(node);
    }
}

编辑:查看?.frame.size是1024 x 768;图像大小相同。

1 个答案:

答案 0 :(得分:0)

在创建模糊纹理后尝试将节点缩放到屏幕比例,代码看起来像这样:

func blurWithCompletion() {
    if let effectNode = scene?.childNode(withName: "effectsNode") as? SKEffectNode {
        let  blur = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputRadius": 10.0]);
    effectNode.filter = blur;
    effectNode.shouldRasterize = true;
    effectNode.shouldEnableEffects = true;

    UIGraphicsBeginImageContextWithOptions((view?.frame.size)!, true, 2.0)
    view?.drawHierarchy(in: (view?.frame)!, afterScreenUpdates: true)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    let node = SKSpriteNode(texture: SKTexture(image: image!));
    node.xScale = UIScreen.main.nativeScale
    node.yScale = UIScreen.main.nativeScale
    effectNode.addChild(node);
}
相关问题