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

objective-c – 桌面Cocoa中的UIGraphicsGetImageFromCurrentImageContext对应物?,第1张

概述我正在尝试创建一个简单的Mac绘图应用程序.在iOS上,我使用UIGraphicsGet ImageFromCurrentImageContext在touchesMoved时更新UIImageView的图像.我现在正试图激活同样的东西,但在Mac应用程序上,我想做: myNSImageView.image = UIGraphicsGetImageFromCurrentImageContext(); 我正在尝试创建一个简单的Mac绘图应用程序.在iOS上,我使用UIGraphicsGet ImageFromCurrentimageContext在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):

解决方法
NSInteger wIDth = 1024;NSInteger height = 768;CGContextRef contextRef =  CGBitmapContextCreate(0,kCGImageAlphaPremultiplIEdFirst); CGContextSetRGBstrokecolor(contextRef,1.0);CGContextFillRect(contextRef,CGRectMake(0.0f,0.0f,height));CGImageRef imageRef = CGBitmapContextCreateImage(contextRef);NSImage* image = [[NSImage alloc] initWithCGImage:imageRef size:NSMakeSize(wIDth,height)];CFRelease(imageRef);CFRelease(contextRef);
总结

以上是内存溢出为你收集整理的objective-c – 桌面Cocoa中的UIGraphicsGetImageFromCurrentImageContext对应物?全部内容,希望文章能够帮你解决objective-c – 桌面Cocoa中的UIGraphicsGetImageFromCurrentImageContext对应物?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1001200.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-21
下一篇2022-05-21

发表评论

登录后才能评论

评论列表(0条)

    保存