【发布时间】: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