Mac OS X:绘制到屏幕的NSGraphicsContext使用CGContextRef C函数没有效果。为什么? [英] Mac OS X: Drawing into an offscreen NSGraphicsContext using CGContextRef C functions has no effect. Why?

查看:3270
本文介绍了Mac OS X:绘制到屏幕的NSGraphicsContext使用CGContextRef C函数没有效果。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mac OS X 10.7.4



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



当我使用 NSBezierPath class ,一切正常工作。



然而,当我使用 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];

//设置屏幕上下文
NSGraphicsContext * g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
[NSGraphicsContext setCurrentContext:g];

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

CGContextRef ctx = [g graphicsPort];

//锁定并绘制
[img lockFocus];

//用Cocoa绘制第一行。这工作!
NSPoint p1 = NSMakePoint(NSMaxX(imgRect),NSMinY(imgRect));
NSPoint p2 = NSMakePoint(NSMinX(imgRect),NSMaxY(imgRect));
[NSBezierPath strokeLineFromPoint:p1 toPoint:p2];

//用Core Graphics绘制第二行。这不工作!
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx,0.0,0.0);
CGContextAddLineToPoint(ctx,imgSize.width,imgSize.height);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);

[img unlockFocus];


解决方案

您不指定如何查看结果。我假设你正在查看 NSImage img 而不是 NSBitmapImageRep [img lockFocus] 时, offscreenRep c>,你正在改变当前的 NSGraphicsContext 作为上下文来绘制 img 。因此, NSBezierPath 绘制到 img ,这就是你看到的。 CG绘图进入 offscreenRep ,你不在看。



而不是将焦点锁定到NSImage创建一个NSImage并添加offscreenRep作为它的一个reps。

  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];

//设置屏幕上下文
NSGraphicsContext * g = [NSGraphicsContext graphicsContextWithBitmapImageRep:offscreenRep];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];

//用Cocoa绘制第一个笔划
NSPoint p1 = NSMakePoint(NSMaxX(imgRect),NSMinY(imgRect));
NSPoint p2 = NSMakePoint(NSMinX(imgRect),NSMaxY(imgRect));
[NSBezierPath strokeLineFromPoint:p1 toPoint:p2];

//使用Core Graphics绘制第二行程
CGContextRef ctx = [g graphicsPort];
CGContextBeginPath(ctx);
CGContextMoveToPoint(ctx,0.0,0.0);
CGContextAddLineToPoint(ctx,imgSize.width,imgSize.height);
CGContextClosePath(ctx);
CGContextStrokePath(ctx);

//做绘图,所以设置当前上下文回到它是什么
[NSGraphicsContext restoreGraphicsState];

//创建一个NSImage并将其添加到它
NSImage * img = [[[NSImage alloc] initWithSize:imgSize] autorelease];
[img addRepresentation:offscreenRep];

//然后继续保存或查看NSImage


Mac OS X 10.7.4

I am drawing into an offscreen graphics context created via +[NSGraphicsContext graphicsContextWithBitmapImageRep:].

When I draw into this graphics context using the NSBezierPath class, everything works as expected.

However, when I draw into this graphics context using the CGContextRef C functions, I see no results of my drawing. Nothing works.

For reasons I won't get into, I really need to draw using the CGContextRef functions (rather than the Cocoa NSBezierPath class).

My code sample is listed below. I am attempting to draw a simple "X". One stroke using NSBezierPath, one stroke using CGContextRef C functions. The first stroke works, the second does not. What am I doing wrong?

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];

解决方案

You don't specify how you are looking at the results. I assume you are looking at the NSImage img and not the NSBitmapImageRep offscreenRep.

When you call [img lockFocus], you are changing the current NSGraphicsContext to be a context to draw into img. So, the NSBezierPath drawing goes into img and that's what you see. The CG drawing goes into offscreenRep which you aren't looking at.

Instead of locking focus onto an NSImage and drawing into it, create an NSImage and add the offscreenRep as one of its reps.

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

这篇关于Mac OS X:绘制到屏幕的NSGraphicsContext使用CGContextRef C函数没有效果。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆