title: NSBezierPath AddClip to mask NSImage - Size issue
body: <p>On a macOS (OSX) desktop app, I use NSBezierPath to draw a random closed shape which looks like this.</p>
  
  <p><a href="https://i.stack.imgur.com/VGopJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VGopJ.png" alt="enter image description here"></a></p>
  
  <p>As you can see the dashed line shows the closed path which was drawn.</p>
  
  <p>Now am trying to extract the masked image as per this path.
  But i get a small portion of the image.  The masked image seems to correctly get the outline from the BezierPath.  But size is an issue.</p>
  
  <p><a href="https://i.stack.imgur.com/S0lQf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/S0lQf.png" alt="enter image description here"></a></p>
  
  <p>This is the method which returns the masked/clipped image.
  The sourceImage.size for this drawing - 1021 * 1031</p>
  
  <pre><code>  - (NSImage *)imageByApplyingClippingBezierPath:(NSImage *)sourceImage
                                  bezierPath:(NSBezierPath *)bezierPath
                                    newFrame:(NSRect)newFrame
    {
      NSImage* newImage = [[NSImage alloc] initWithSize:newFrame.size];
      NSBitmapImageRep* rep = [[NSBitmapImageRep alloc]
                               initWithBitmapDataPlanes:NULL
                               pixelsWide:newFrame.size.width
                               pixelsHigh:newFrame.size.height
                               bitsPerSample:8
                               samplesPerPixel:4
                               hasAlpha:YES
                               isPlanar:NO
                               colorSpaceName:NSCalibratedRGBColorSpace
                               bytesPerRow:0
                               bitsPerPixel:0];
  
      [newImage addRepresentation:rep];
  
      [newImage lockFocus];
  
      CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
      CGContextSaveGState(context);
  
      [bezierPath addClip];
  
      NSRect targetFrame = NSMakeRect(0, 0, newFrame.size.width, newFrame.size.height);
      [sourceImage drawInRect:targetFrame];
  
      [newImage unlockFocus];
  
      CGContextRestoreGState(context);
  
      return newImage;
  }
  </code></pre>
  
  <p>How can i get perfectly sized image outlined by the BezierPath?
  Any tips would be appreciated!</p>
  
  <h2>UPDATE</h2>
  
  <p>Just clarifying, how i draw the image.
  I get rectangle bounds of the bezier path and rectangular image cropped  like this. </p>
  
  <pre><code>CGRect bezierBounds = CGPathGetPathBoundingBox([self.smartLassoWavyBezierPath quartzPath]);
   NSRect targetFrame = NSMakeRect(0, 0, bezierBounds.size.width, bezierBounds.size.height);
   NSImage *targetImage = [[NSImage alloc initWithSize:targetFrame.size];
  [targetImage lockFocus];
  [self.view.originalLoadImageOnCanvas 
  drawInRect:targetFrame fromRect:bezierBounds operation:NSCompositeCopy 
   fraction:1.0f];
  [targetImage unlockFocus];
  </code></pre>