CGContext Background Color Change

I'm really new to working with CGContexts, and I'm just trying to figure things out by following simple drawing and sketching instructions (http://blog.effectiveui.com/?p=8105)

I hit a brick wall when it came to changing the background color of my CGContext.

I run the context as follows:

- (BOOL) initContext:(CGSize)size { int bitmapBytesPerRow; bitmapBytesPerRow = (size.width * 4); cacheContext = CGBitmapContextCreate (nil, size.width, size.height, 8, bitmapBytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipLast); CGContextSetRGBFillColor(cacheContext, 1, 1, 1, 1); return YES; } 

and changing the color and width of the beat:

 UIColor *color = [UIColor whiteColor]; CGContextSetStrokeColorWithColor(cacheContext, [color CGColor]); CGContextSetLineCap(cacheContext, kCGLineCapRound); CGContextSetLineWidth(cacheContext, 4); 

but when I try to change the background color from black (either in init, or in the parts of the drawing / installation setup) using CGContextSetRGBFillColor(cacheContext, 1, 1, 1, 1); , the effect will not be.

Can someone point me in the right direction, either the best / right place to put this call, or the right call? Thanks so much for your time!

+6
source share
1 answer

You cannot just set the color and expect the context to fill with a new color. You must draw this color in context. In the init method, after setting the fill color, try using something like

 CGContextFillRect(cacheContext, (CGRect){CGPointZero, size}); 

In the corresponding note, the context is not black by default. They have a transparent color. Now, if your context does not have an alpha channel, then it will be black, but if your context has an alpha channel and you see black, this is because you are outputting on top of something black (or, conversely, you put a transparent image into a layer with the opaque flag set to true, and CoreAnimation finishes painting over black).

+22
source

Source: https://habr.com/ru/post/906718/


All Articles