3

I created a CAGradientLayer, which works really fine. But my problem is, that I want to use it in SpriteKit as a background...

Is there a possibility to realize it ? Maybe by putting it in a SKSpriteNode ?

Thanks in advance

EDIT:

Unfortunately theres an error message:

May 28 19:08:19 amirs-mbp Gradient[2845] : CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. May 28 19:08:19 amirs-mbp Gradient[2845] : CGContextSaveGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. May 28 19:08:19 amirs-mbp Gradient[2845] : CGContextClipToRect: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. May 28 19:08:19 amirs-mbp Gradient[2845] : CGContextTranslateCTM: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. May 28 19:08:19 amirs-mbp Gradient[2845] : CGContextScaleCTM: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. May 28 19:08:19 amirs-mbp Gradient[2845] : CGContextDrawLinearGradient: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. May 28 19:08:19 amirs-mbp Gradient[2845] : CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update. May 28 19:08:19 amirs-mbp Gradient[2845] : CGContextRestoreGState: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.

appcodix
  • 342
  • 2
  • 15
  • Refer this https://stackoverflow.com/questions/18894493/making-a-skscenes-background-transparent-not-working-is-this-a-bug/24494346#24494346 – Suresh Oct 05 '16 at 14:42

3 Answers3

2

I think you'll have much better luck with this:

https://github.com/braindrizzlestudio/BDGradientNode

Ian McKay
  • 76
  • 7
1

First convert the layer to UIImage*:

- (UIImage *)imageFromLayer:(CALayer *)layer
{

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions([layer frame].size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext([layer frame].size);

    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return outputImage;
}

And then init a SKSpriteNode with it:

SKSpriteNode* node = [SKSpriteNode spriteNodeWithColor:[UIColor colorWithPatternImage:image] 
                                   size:yourLayer.bounds.size]
gabuh
  • 1,166
  • 8
  • 15
  • Thanks for your quick response :) Unfortunately it doesn't work... I've got an error message, which I posted above as EDIT – appcodix May 28 '14 at 17:14
  • It must be that you are creating the context in the wrong place. you'll need to locate your drawing code within the -drawRect: method of a UIView (or –drawInContext: of a CALayer). That's because the currentcontext is nil until it's required. – gabuh May 28 '14 at 17:57
  • Forget my last comment... it's weird because UIGraphicsGetCurrentContext() should not be nil between UIGraphicsBeginImageContext and UIGraphicsEndImageContext... Maybe the cause is the interaction with SpriteKit. – gabuh May 28 '14 at 18:03
0

This function works for me for creating a SpriteNode from CAGradientLayer. Please note it is a function of GameScene, so that is the 'self.frame' context that it is referring to get the right size of the gradient.

func layerToSKSpriteNode(layer : CALayer) -> SKSpriteNode
{
    var view = UIView()
    layer.frame = self.frame
    view.layer.addSublayer(layer)

    UIGraphicsBeginImageContext(self.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    var bgImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    return SKSpriteNode(texture: SKTexture(image: bgImage))
} 
OwlOCR
  • 1,127
  • 11
  • 22