Mac OS X:使用CGContextRef绘制到屏幕外的NSGraphicsContext C函数无效。为什么?

时间:2012-05-16 22:19:09

标签: objective-c macos cocoa core-graphics quartz-2d

Mac OS X 10.7.4

我正在绘制通过+[NSGraphicsContext graphicsContextWithBitmapImageRep:]创建的屏幕外图形上下文。

当我使用 NSBezierPath绘制到此图形上下文时,一切都按预期工作。

但是,当我使用 CGContextRef C函数绘制到此图形上下文时,我看不到绘图的结果。什么都行不通。

由于我不会进入的原因,我真的需要使用CGContextRef函数(而不是Cocoa NSBezierPath类)。

我的代码示例如下所示。我试图画一个简单的" X"。一次使用NSBezierPath,一次使用CGContextRef C函数。第一笔中风起作用,第二笔中风不起作用。我做错了什么?

NSRect imgRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
NSSize imgSize = imgRect.size;

NSBitmapImageRep *offscreenRep = [[[NSBitmapImageRep alloc]
   initWithBitmapDataPlanes:NULL
   pixelsWide:imgSize.width
   pixelsHigh:imgSize.height
   bitsPerSample:8
   samplesPerPixel:4
   hasAlpha:YES
   isPlanar:NO
   colorSpaceName:NSDeviceRGBColorSpace
   bitmapFormat:NSAlphaFirstBitmapFormat
   bytesPerRow:0
   bitsPerPixel:0] autorelease];

// set offscreen context
NSGraphicsContext *g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
[NSGraphicsContext setCurrentContext:g];

NSImage *img = [[[NSImage alloc] initWithSize:imgSize] autorelease];

CGContextRef ctx = [g graphicsPort];

// lock and draw
[img lockFocus];

// draw first stroke with Cocoa. this works!
NSPoint p1 = NSMakePoint(NSMaxX(imgRect), NSMinY(imgRect));
NSPoint p2 = NSMakePoint(NSMinX(imgRect), NSMaxY(imgRect));
[NSBezierPath strokeLineFromPoint:p1 toPoint:p2];

// draw second stroke with Core Graphics. This doesn't work!
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 0.0, 0.0);
CGContextAddLineToPoint(ctx, imgSize.width, imgSize.height);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);

[img unlockFocus];

5 个答案:

答案 0 :(得分:25)

您没有指定查看结果的方式。我假设您正在查看NSImage img而不是NSBitmapImageRep offscreenRep

当您致电[img lockFocus]时,您正在将当前NSGraphicsContext更改为要吸引到img的上下文。因此,NSBezierPath绘图进入img,这就是您所看到的。 CG绘图进入你不看的offscreenRep

不是将焦点锁定在NSImage上并绘制到NSImage中,而是创建一个NSImage并将offscreenRep添加为其代表之一。

NSRect imgRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
NSSize imgSize = imgRect.size;

NSBitmapImageRep *offscreenRep = [[[NSBitmapImageRep alloc]
   initWithBitmapDataPlanes:NULL
   pixelsWide:imgSize.width
   pixelsHigh:imgSize.height
   bitsPerSample:8
   samplesPerPixel:4
   hasAlpha:YES
   isPlanar:NO
   colorSpaceName:NSDeviceRGBColorSpace
   bitmapFormat:NSAlphaFirstBitmapFormat
   bytesPerRow:0
   bitsPerPixel:0] autorelease];

// set offscreen context
NSGraphicsContext *g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];

// draw first stroke with Cocoa
NSPoint p1 = NSMakePoint(NSMaxX(imgRect), NSMinY(imgRect));
NSPoint p2 = NSMakePoint(NSMinX(imgRect), NSMaxY(imgRect));
[NSBezierPath strokeLineFromPoint:p1 toPoint:p2];

// draw second stroke with Core Graphics
CGContextRef ctx = [g graphicsPort];    
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 0.0, 0.0);
CGContextAddLineToPoint(ctx, imgSize.width, imgSize.height);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);

// done drawing, so set the current context back to what it was
[NSGraphicsContext restoreGraphicsState];

// create an NSImage and add the rep to it    
NSImage *img = [[[NSImage alloc] initWithSize:imgSize] autorelease];
[img addRepresentation:offscreenRep];

// then go on to save or view the NSImage

答案 1 :(得分:6)

我想知道为什么每个人都会编写如此复杂的代码来绘制图像。除非你关心图像的确切位图表示(通常你不关心!),否则不需要创建一个。您可以创建一个空白图像并直接绘制到它。在这种情况下,系统将创建一个适当的位图表示(或者可能是PDF表示或系统认为更适合绘图的任何内容)。

init方法的文档

- (instancetype)initWithSize:(NSSize)aSize

自MacOS 10.0以来一直存在,但仍未被弃用,明确表示:

  

使用此方法初始化图像对象后,即可   期望在尝试绘制之前提供图像内容   图片。 您可能会将焦点锁定在图像上并绘制到图像或您   可能会明确添加您创建的图像表示。

所以这就是我编写该代码的方式:

NSRect imgRect = NSMakeRect(0.0, 0.0, 100.0, 100.0);
NSImage * image = [[NSImage alloc] initWithSize:imgRect.size];

[image lockFocus];
// draw first stroke with Cocoa
NSPoint p1 = NSMakePoint(NSMaxX(imgRect), NSMinY(imgRect));
NSPoint p2 = NSMakePoint(NSMinX(imgRect), NSMaxY(imgRect));
[NSBezierPath strokeLineFromPoint:p1 toPoint:p2];

// draw second stroke with Core Graphics
CGContextRef ctx = [[NSGraphicsContext currentContext] graphicsPort];
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx, 0.0, 0.0);
CGContextAddLineToPoint(ctx, imgRect.size.width, imgRect.size.height);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);
[image unlockFocus];

这就是所有人。

graphicsPort实际上是void *

@property (readonly) void * graphicsPort 

并记录为

  

表示低级,特定于平台的图形上下文   通过图形端口。

这几乎可能是一切,但最后的说明是

  

在OS X中,这是Core Graphics上下文,   一个CGContextRef对象(不透明类型)。

此属性已在10.10中弃用,以支持新属性

@property (readonly) CGContextRef CGContext

仅适用于10.10及更高版本。如果您必须支持较旧的系统,仍然可以使用graphicsPort

答案 2 :(得分:4)

@Robin Stewart的解决方案对我来说效果很好。我能够将其压缩为NSImage扩展。

extension NSImage {
    convenience init(size: CGSize, actions: (CGContext) -> Void) {
        self.init(size: size)
        lockFocusFlipped(false)
        actions(NSGraphicsContext.current!.cgContext)
        unlockFocus()
    }
}

用法:

let image = NSImage(size: CGSize(width: 100, height: 100), actions: { ctx in
    // Drawing commands here for example:
    // ctx.setFillColor(.white)
    // ctx.fill(pageRect)
})

答案 3 :(得分:1)

以下是绘制同一图像的三种方法(快速4)。

@Mecki建议的方法所产生的图像没有blurring伪像(如模糊曲线)。但这可以通过调整CGContext设置(此示例中未包括)来解决。

public struct ImageFactory {

   public static func image(size: CGSize, fillColor: NSColor, rounded: Bool = false) -> NSImage? {
      let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
      return drawImage(size: size) { context in
         if rounded {
            let radius = min(size.height, size.width)
            let path = NSBezierPath(roundedRect: rect, xRadius: 0.5 * radius, yRadius: 0.5 * radius).cgPath
            context.addPath(path)
            context.clip()
         }
         context.setFillColor(fillColor.cgColor)
         context.fill(rect)
      }
   }

}

extension ImageFactory {

   private static func drawImage(size: CGSize, drawingCalls: (CGContext) -> Void) -> NSImage? {
      return drawImageInLockedImageContext(size: size, drawingCalls: drawingCalls)
   }

   private static func drawImageInLockedImageContext(size: CGSize, drawingCalls: (CGContext) -> Void) -> NSImage? {
      let image = NSImage(size: size)
      image.lockFocus()
      guard let context = NSGraphicsContext.current else {
         image.unlockFocus()
         return nil
      }
      drawingCalls(context.cgContext)
      image.unlockFocus()
      return image
   }

   // Has scalling or antialiasing issues, like blurred curves.
   private static func drawImageInBitmapImageContext(size: CGSize, drawingCalls: (CGContext) -> Void) -> NSImage? {
      guard let offscreenRep = NSBitmapImageRep(pixelsWide: Int(size.width), pixelsHigh: Int(size.height),
                                                bitsPerSample: 8, samplesPerPixel: 4, hasAlpha: true,
                                                isPlanar: false, colorSpaceName: .deviceRGB) else {
                                                   return nil
      }
      guard let context = NSGraphicsContext(bitmapImageRep: offscreenRep) else {
         return nil
      }
      NSGraphicsContext.saveGraphicsState()
      NSGraphicsContext.current = context
      drawingCalls(context.cgContext)
      NSGraphicsContext.restoreGraphicsState()
      let img = NSImage(size: size)
      img.addRepresentation(offscreenRep)
      return img
   }

   // Has scalling or antialiasing issues, like blurred curves.
   private static func drawImageInCGContext(size: CGSize, drawingCalls: (CGContext) -> Void) -> NSImage? {
      let colorSpace = CGColorSpaceCreateDeviceRGB()
      let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
      guard let context = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: 8,
                                    bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue) else {
         return nil
      }
      drawingCalls(context)
      guard let image = context.makeImage() else {
         return nil
      }
      return NSImage(cgImage: image, size: size)
   }
}

答案 4 :(得分:0)

Swift 4::我使用以下代码,该代码从UIKit复制了便捷的API(但在macOS上运行):

int main()
{
    for(int i=33; i<=47 ;i++)
    {
        std::cout << (char)i;
    }
    for(int j=58; j<=64;j++)
    {
        std::cout << (char)j;
    }
    for(int z=91; z<=96;z++)
    {
        std::cout << (char)z;
    }
    return 0;
}

用法:

public class UIGraphicsImageRenderer {
    let size: CGSize

    init(size: CGSize) {
        self.size = size
    }

    func image(actions: (CGContext) -> Void) -> UIImage {
        let image = NSImage(size: size)
        image.lockFocusFlipped(true)
        actions(NSGraphicsContext.current!.cgContext)
        image.unlockFocus()
        return image
    }
}