macOS Use CGContextRef and always black











up vote
0
down vote

favorite












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 hereenter 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.










share|improve this question




























    up vote
    0
    down vote

    favorite












    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 hereenter 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.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 hereenter 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.










      share|improve this question















      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 hereenter 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.







      macos core-graphics nsbezierpath






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 8 at 8:32

























      asked Nov 8 at 8:16









      Shucheng

      45110




      45110
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Unlike UIBezierPath, appendBezierPathWithArcWithCenter is measured in degrees.






          share|improve this answer








          New contributor




          Newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Yes, I found it and fixed this problem. Thanks
            – Shucheng
            2 days ago











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203731%2fmacos-use-cgcontextref-and-always-black%23new-answer', 'question_page');
          }
          );

          Post as a guest
































          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          0
          down vote



          accepted










          Unlike UIBezierPath, appendBezierPathWithArcWithCenter is measured in degrees.






          share|improve this answer








          New contributor




          Newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Yes, I found it and fixed this problem. Thanks
            – Shucheng
            2 days ago















          up vote
          0
          down vote



          accepted










          Unlike UIBezierPath, appendBezierPathWithArcWithCenter is measured in degrees.






          share|improve this answer








          New contributor




          Newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • Yes, I found it and fixed this problem. Thanks
            – Shucheng
            2 days ago













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Unlike UIBezierPath, appendBezierPathWithArcWithCenter is measured in degrees.






          share|improve this answer








          New contributor




          Newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          Unlike UIBezierPath, appendBezierPathWithArcWithCenter is measured in degrees.







          share|improve this answer








          New contributor




          Newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          Newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered 2 days ago









          Newbie

          16




          16




          New contributor




          Newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          Newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          Newbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • Yes, I found it and fixed this problem. Thanks
            – Shucheng
            2 days ago


















          • Yes, I found it and fixed this problem. Thanks
            – Shucheng
            2 days ago
















          Yes, I found it and fixed this problem. Thanks
          – Shucheng
          2 days ago




          Yes, I found it and fixed this problem. Thanks
          – Shucheng
          2 days ago


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53203731%2fmacos-use-cgcontextref-and-always-black%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          Popular posts from this blog

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff