【问题标题】:UIGraphicsGetImageFromCurrentImageContext counterpart in desktop Cocoa?桌面可可中的 UIGraphicsGetImageFromCurrentImageContext 对应项?
【发布时间】:2011-12-18 18:08:07
【问题描述】:

我正在尝试创建一个简单的 Mac 绘画应用程序。在 iOS 上,我使用 UIGraphicsGetImageFromCurrentImageContext 在 touchesMoved 时更新 UIImageView 的图像。我现在正在尝试激活相同的功能,但在 Mac 应用程序上,我想做:

myNSImageView.image = UIGraphicsGetImageFromCurrentImageContext(); 

但该功能仅存在于 cocoa-touch 上,而不存在于 cocoa 框架内,关于在哪里/是否可以在 Cocoa 中找到类似功能的任何提示?

谢谢。

更新

一些额外的代码以确保完整性。

- (void)mouseDragged:(NSEvent *)event
{

    NSInteger width = canvasView.frame.size.width;
    NSInteger height = canvasView.frame.size.height;

    NSGraphicsContext * graphicsContext = [NSGraphicsContext currentContext];
    CGContextRef contextRef = (CGContextRef) [graphicsContext graphicsPort];

    CGContextRef imageContextRef =  CGBitmapContextCreate(0, width, height, 8, width*4,    [NSColorSpace genericRGBColorSpace].CGColorSpace, kCGImageAlphaPremultipliedFirst); 

    NSPoint currentPoint = [event locationInWindow];

    CGContextSetRGBStrokeColor(contextRef, 49.0/255.0, 147.0/255.0, 239.0/255.0, 1.0);
    CGContextSetLineWidth(contextRef, 1.0);

    CGContextBeginPath(contextRef);
    CGContextMoveToPoint(contextRef, lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(contextRef, currentPoint.x, currentPoint.y);
    CGContextDrawPath(contextRef,kCGPathStroke);

    CGImageRef imageRef = CGBitmapContextCreateImage(imageContextRef);
    NSImage* image = [[NSImage alloc] initWithCGImage:imageRef size:NSMakeSize(width, height)];
    canvasView.image = image;
    CFRelease(imageRef);
    CFRelease(imageContextRef);

    lastPoint = currentPoint;

}

我现在得到的结果是,这幅画在视图的上部工作正常,但其余部分却不正常。就像这张图片(红色部分应该是canvasView):

【问题讨论】:

  • 你说它“不工作” - 也许你应该改进并告诉我们到底是什么问题。
  • @直到问题很清楚,他提到的功能只有iOS,他正在寻找一个mac等效
  • @jrturton 感谢您的澄清,修改了问题以向所有人指出这一点。
  • 感谢您的 cmets,为了完整起见,我添加了一些额外的代码。

标签: objective-c ios cocoa core-graphics


【解决方案1】:
NSInteger width = 1024;
NSInteger height = 768;
CGContextRef contextRef =  CGBitmapContextCreate(0, width, height, 8, width*4, [NSColorSpace genericRGBColorSpace].CGColorSpace, kCGImageAlphaPremultipliedFirst); 

CGContextSetRGBStrokeColor(contextRef, 49.0/255.0, 147.0/255.0, 239.0/255.0, 1.0);
CGContextFillRect(contextRef, CGRectMake(0.0f, 0.0f, width, height));

CGImageRef imageRef = CGBitmapContextCreateImage(contextRef);
NSImage* image = [[NSImage alloc] initWithCGImage:imageRef size:NSMakeSize(width, height)];
CFRelease(imageRef);
CFRelease(contextRef);

【讨论】:

  • 谢谢,没有让它工作,我没有“被允许”创建一个 CGImageRef 的 CGBitmapContextCreate。
  • 我改变了我正在做的事情,让我知道这是否对你有用。这至少应该为您提供有效的上下文和 nsimage。
  • 谢谢,几乎可以使用了。我用你的代码编辑了我的答案,但现在我遇到了另一个奇怪的问题。 :)
  • 在您的示例中,您没有在 imageContextRef 中绘制任何内容,请尝试使用您的 imageContextRef 代替您的 contextRef
  • 我试图改变,例如。 CGContextBeginPath(contextRef);到 CGContextBeginPath(imageContextRef) 和另一个,但它不会在任何地方绘制任何东西。嗯。
【解决方案2】:
NSGraphicsContext * graphicsContext = [NSGraphicsContext currentContext];
CGContextRef contextRef = (CGContextRef) [graphicsContext graphicsPort];

CGContextRef imageContextRef =  CGBitmapContextCreate(0, width, height, 8, width*4,    [NSColorSpace genericRGBColorSpace].CGColorSpace, kCGImageAlphaPremultipliedFirst); 

您首先假设存在一个当前上下文,然后您创建一个单独的上下文。

CGContextSetRGBStrokeColor(contextRef, 49.0/255.0, 147.0/255.0, 239.0/255.0, 1.0);
CGContextSetLineWidth(contextRef, 1.0);

(etc.)

然后你绘制到你假设存在的上下文中,而不是你创建的上下文中。

CGImageRef imageRef = CGBitmapContextCreateImage(imageContextRef);

然后你从你创建但从未绘制的上下文中创建一个图像。

停止获取[NSGraphicsContext currentContext] 及其图形端口的尝试。无论如何,您不能假设在drawRect: 之外存在当前上下文。相反,请绘制到您创建的上下文中。

【讨论】:

  • 我尝试将 contextRef 替换为 imageContextRef,但它没有绘制任何内容。我应该澄清的是,绘图发生在 NSViewController 中,我创建了一个自定义子类 NSImageView 与委托方法 mouseDown 等。我将此自定义类作为子视图添加到视图控制器视图中。我认为绘图在 NSImageView 之外工作(尽管我必须从其中拖动鼠标以触发委托方法)但不在内部工作,这很奇怪。
  • @Anders:“我尝试将 contextRef 替换为 imageContextRef,但它没有绘制任何内容。”它现在不绘制任何东西——不在您创建并尝试从中提取图像的上下文中。您正在从没有任何内容的上下文中提取图像。此外,您正在进入的上下文,您不能保证能够获得。你应该只在视图的drawRect: 方法中绘制到当前上下文(或者在设置当前上下文之后,除非在极少数情况下这不是必需的);在其他任何地方,唯一正确的绘制方法是根据您创建的上下文。
  • 好的,感谢您的建议和提示。我将尝试改变我的方法并将我的绘图代码移动到我的 NSImageView 本身,它是 drawrect。发现这个似乎有效的例子,juliuspaintings.co.uk/cgi-bin/paint_css/animatedPaint/…。谢谢。 :)
猜你喜欢
  • 1970-01-01
  • 2022-01-21
  • 2010-09-12
  • 2019-03-03
  • 2010-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多