3

I'm trying to use the screenshot approach to add a blurred image of my background to the scene -- as outlined briefly here and elsewhere.

However, on my retina screen, the image that is overlaid is 1024 x 768 and not 2048 x 1536. This makes it appear incredibly small on the screen. How do I overlay the right sized image by adjusting this code?

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

EDIT: view?.frame.size is 1024 x 768; the image size is the same.

Community
  • 1
  • 1
  • Edit your question and add what view?.frame.size! says. Do the same for the `image` (`image.size`). Also change this line `UIGraphicsBeginImageContextWithOptions((view?.frame.size)!, true, 2.0)` to this : `UIGraphicsBeginImageContextWithOptions((view?.frame.size)!, true, 0.0)` – Whirlwind May 09 '17 at 11:17
  • @Whirlwind Thanks, see above. –  May 09 '17 at 11:52
  • Is this before or after you have set the scale parameter to 0.0 ? – Whirlwind May 09 '17 at 12:19
  • This is before. Setting the parameter to 2.0 doesn't change it though. –  May 09 '17 at 12:41

1 Answers1

0

Try scaling the node to the screen scale after the blurred texture is created, code would look like something like this:

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);
}
carlito
  • 64
  • 4