【问题标题】:Adding blur to scene in SpriteKit causes issues with image size在 SpriteKit 中为场景添加模糊会导致图像大小出现问题
【发布时间】:2017-05-09 10:36:02
【问题描述】:

我正在尝试使用屏幕截图的方法将我的背景的模糊图像添加到场景中——正如在 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);
    }
}

编辑:view?.frame.size 为 1024 x 768;图像大小相同。

【问题讨论】:

  • 编辑您的问题并添加什么视图?.frame.size!说。对image (image.size) 执行相同的操作。还要将此行 UIGraphicsBeginImageContextWithOptions((view?.frame.size)!, true, 2.0) 更改为:UIGraphicsBeginImageContextWithOptions((view?.frame.size)!, true, 0.0)
  • @Whirlwind 谢谢,见上文。
  • 这是在您将 scale 参数设置为 0.0 之前还是之后?
  • 这是以前的。将参数设置为 2.0 并不会改变它。

标签: swift sprite-kit


【解决方案1】:

在创建模糊纹理后尝试将节点缩放到屏幕比例,代码如下所示:

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);
}

【讨论】:

  • 这样做会导致整个屏幕变黑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多