开发者

Unable to draw gradient into a layer CGLayerRef

开发者 https://www.devze.com 2023-04-07 03:36 出处:网络
I am new to Xcode and iOS development. I am trying to draw a gradient into a layer so that I can draw this layer repeatedly in my view. This gradient forms the background of the view and I do some dra

I am new to Xcode and iOS development. I am trying to draw a gradient into a layer so that I can draw this layer repeatedly in my view. This gradient forms the background of the view and I do some drawing over this gradient. But when I draw a gradient into my layer, and then draw it in my view's context, it does not draw the gradient. I have tried debugging the code but everything seems to be just fine. Pasting the relevant code below: I create the gradientLayer at the start.

CGContextRef context = UIGraphicsGetCurrentContext();
CGRect myRect = self.bounds;
CGGradientRef myGradient;
CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
CGFloat colors[] =
{
    204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00,
    100 / 255.0, 200 / 255.0, 50 / 255.0, 1.00,
    0, 0, 0, 1.00,
};
myGradient = CGGradientCreateWithColorComponents(rgb, colors, NULL, sizeof(colors)/(sizeof(colors[0])*4));
CGColorSpaceRelease(rgb);

CGContextRef layerContext = CGLayerGetContext(gradientLayer);
CGPoint start, end;
CGRect clip = CGContextGetClipBoundingBox(layerContext);
start = getStartPoint(clip);
end = getEndPoint(clip);
CGContextDrawLinearGradient(layerContext, myGradient, start, end, 0);
//CGContextSetRGBFillColor (layerContext, red / 255.0, green / 255.0, blue / 255.0, 1);
//CGContextFillRect (layerContext, clip);

I call this function when my class is instantiated. And then draw this layer repeatedly in my drawRect method.

CGRect rect = self.bounds;
CGContextSaveGState(context);
CGContextDrawLayerInRect(context, rect, gradientLayer);
CGContextSaveGState(context);

I can draw the same gradient directly in the drawRect b开发者_如何学JAVAut am unable to do so when drawing into a layer and then in the drawRect(trying to optimize the drawing).


The RGBA values for your gradient should be between 0.0f and 1.0f not between 0.0f and 255.0f.


YOU need to convert CGLayerRef into CGContextRef Like this:

    CGContextRef context = UIGraphicsGetCurrentContext();
     CGLayerRef layer = CGLayerCreateWithContext(context, myRect.size, NULL);
        CGContextRef gradientContext = CGLayerGetContext(layer);
// from here we care only on this  gradientContext
    // somewhere call 
    CGContextDrawLinearGradient(gradientContext, /*other args*/ 

// don't forget to clean memory 

CGLayerRelease(layer);

and you can draw the gradient onto CGContextRef context

Or if you want to escape those conversions, you can use the normal CGContextRef

Here is how I achieved the gradient

CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGColorSpaceRef rgb = CGColorSpaceCreateDeviceRGB();
   // CGLayerRef gradientLayer = CGLayerCreateWithContext(ctx, myRect.size, NULL);
    CGFloat colors[] =
    {
        204.0 / 255.0, 224.0 / 255.0, 244.0 / 255.0, 1.00, //color 1
        100 / 255.0, 200 / 255.0, 50 / 255.0, 1.00, // color 2
        0.0, 0.0, 0.0, 1.00, // color3 
    };
     CGFloat locations[3] = { 0.0, 0.5, 1.0 }; // 3 locations
    CGGradientRef   myGradient = CGGradientCreateWithColorComponents(rgb, colors, locations, 3);
    CGColorSpaceRelease(rgb);
  //  CGContextRef layerContext = CGLayerGetContext(gradientLayer);
    CGFloat minX = CGRectGetMinX(self.bounds);
    CGFloat minY = CGRectGetMinY(self.bounds);
    CGFloat maxY = CGRectGetMaxY(self.bounds);
    CGContextBeginPath(ctx);
    CGRect clip = self.bounds;
    CGContextAddRect(ctx, clip);
    //release memory
    CGContextClip(ctx);
    CGContextDrawLinearGradient(ctx, myGradient, CGPointMake(minX,minY), CGPointMake(minX,maxY), kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
    CGGradientRelease(myGradient);

Use the above code to get the gradient, Ofcourse you can tweak both locations and colors to get the effect you want, but remember the locations always is between 0.0 and 1.0

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号