macOS Use CGContextRef and always black

I try to use this solution to build a gradient circle by @Johnny Rockex on macOS.But I got some problems.

In the @Johnny Rockex original answer

//3.1 setup

UIBezierPath * bezierPath;
CGPoint center = CGPointMake(dimension/2, dimension/2);

UIGraphicsBeginImageContextWithOptions(CGSizeMake(dimension, dimension), true, 0.0);
UIRectFill(CGRectMake(0, 0, dimension, dimension));

this method is provided by UIKit rather than AppKit. So I use some methods by AppKit

NSImage *contentView  =[[NSImage alloc]initWithSize:NSMakeSize(dimension, dimension)];

[contentView lockFocus];
[NSGraphicsContext currentContext].imageInterpolation = NSImageInterpolationNone;
CGContextRef gc = [[NSGraphicsContext currentContext]CGContext];
NSRectFill(CGRectMake(0, 0, dimension, dimension));

//fill Colors in bazierPath
for (int n = 0; n<numberOfColors;n++) {

[contentView unlockFocus];
[radialGradient setImage:contentView];

Unfortunately, I got a weird result at lastenter image description here enter image description here

My colors array and bazierPath is correct but only a little part of the image has color. Maybe is not caused by the title despection but I can not find any clue in other parts of codes.

1 answer