gpt4 book ai didi

objective-c - 桌面 Cocoa 中的 UIGraphicsGetImageFromCurrentImageContext 对应物?

转载 作者:可可西里 更新时间:2023-11-01 05:42:26 24 4
gpt4 key购买 nike

我正在尝试创建一个简单的 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;

我现在得到的结果是,这幅画在 View 的上部效果很好,但其余部分却很糟糕。就像这张图片(红色部分应该是 canvasView):

enter image description here

最佳答案

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

关于objective-c - 桌面 Cocoa 中的 UIGraphicsGetImageFromCurrentImageContext 对应物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8553668/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com