在 Cocoa 中以编程方式编辑图像?

发布于 2024-10-24 17:59:14 字数 110 浏览 1 评论 0原文

我想为我的应用程序设置自定义鼠标光标,并希望通过用自定义颜色替换白色边框来以编程方式将默认光标的颜色更改为自定义颜色。问题是我什至不知道从哪里开始在 Cocoa 中以编程方式编辑图像,因此非常感谢任何帮助!

I want to set a custom mouse cursor for my app and would like to programmatically change the default cursor's color to a custom color by replacing the white border with the custom color. The problem is that I don't even know where to start to programmatically edit images in Cocoa, so any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゝ杯具 2024-10-31 17:59:14

您可以使用 -[NSCursor arrowCursor] 获取默认光标。有了光标后,您可以使用 -[NSCursor image] 获取其图像。您不应该修改另一个对象的图像,因此您应该复制该图像。然后您应该编辑图像,并使用 -[NSCursor initWithImage:hotSpot:] 创建一个新光标。您的代码应如下所示:

- (NSImage *)customArrowCursorImage {
    NSImage *image = [[[NSCursor arrowCursor] image] copy];
    [image lockFocus];
    /// Do custom drawing
    [image unlockFocus];
}

- (NSCursor *)customArrowCursor {
    NSImage *image = [self customArrowCursorImage];
    NSPoint hotSpot = [[NSCursor arrowCursor] hotSpot];
    return [[[NSCursor alloc] initWithImage:image hotSpot:hotSpot] autorelease];
}

您应该能够使用核心图像过滤器将图像的白色替换为自定义颜色。但如果您只是想开始,可以使用 NSReadPixel() 和 NSRectFill 一次为一个像素着色。像使用 NSReadPixel 和 NSRectFill 那样每次绘制一个像素会非常慢,因此您应该这样做只是为了了解所有这些是如何工作的。

You can get the default cursor with -[NSCursor arrowCursor]. Once you have a cursor, you can get its image with -[NSCursor image]. You shouldn't modify another object's image, so you should copy that image. Then you should edit the image, and create a new cursor with -[NSCursor initWithImage:hotSpot:]. Your code should look something like this:

- (NSImage *)customArrowCursorImage {
    NSImage *image = [[[NSCursor arrowCursor] image] copy];
    [image lockFocus];
    /// Do custom drawing
    [image unlockFocus];
}

- (NSCursor *)customArrowCursor {
    NSImage *image = [self customArrowCursorImage];
    NSPoint hotSpot = [[NSCursor arrowCursor] hotSpot];
    return [[[NSCursor alloc] initWithImage:image hotSpot:hotSpot] autorelease];
}

You should be able to replace the white color of the image with a custom color by using a core image filter. But if you just want to get started, you can use NSReadPixel() and NSRectFill to color one pixel at a time. Drawing one pixel a a time like that with with NSReadPixel and NSRectFill will be exceptionally slow, so you should only do that to get a feel for how all of this works.

故事还在继续 2024-10-31 17:59:14

我的最终代码。这采用标准 IBeam 光标(将鼠标悬停在文本视图上时的光标)并将彩色光标存储在 coloredIBeamCursor 指针中。

- (void)setPointerColor:(NSColor *)newColor {
    // create the new cursor image
    [[NSGraphicsContext currentContext] CIContext];
    // create the layer with the same color as the text
    CIFilter *backgroundGenerator=[CIFilter filterWithName:@"CIConstantColorGenerator"];
    CIColor *color=[[[CIColor alloc] initWithColor:newColor] autorelease];
    [backgroundGenerator setValue:color forKey:@"inputColor"];
    CIImage *backgroundImage=[backgroundGenerator valueForKey:@"outputImage"];
    // create the cursor image
    CIImage *cursor=[CIImage imageWithData:[[[NSCursor IBeamCursor] image] TIFFRepresentation]];
    CIFilter *filter=[CIFilter filterWithName:@"CIColorInvert"];
    [filter setValue:cursor forKey:@"inputImage"];
    CIImage *outputImage=[filter valueForKey:@"outputImage"];
    // apply a multiply filter
    filter=[CIFilter filterWithName:@"CIMultiplyCompositing"];
    [filter setValue:backgroundImage forKey:@"inputImage"];
    [filter setValue:outputImage forKey:@"inputBackgroundImage"];
    outputImage=[filter valueForKey:@"outputImage"];
    // get the NSImage from the CIImage
    NSCIImageRep *rep=[NSCIImageRep imageRepWithCIImage:outputImage];
    NSImage *newImage=[[[NSImage alloc] initWithSize:[outputImage extent].size] autorelease];
    [newImage addRepresentation:rep];
    // remove the old cursor (if any)
    if (coloredIBeamCursor!=nil) {
        [self removeCursorRect:[self visibleRect] cursor:coloredIBeamCursor];
        [coloredIBeamCursor release];
    }
    // set the new cursor
    NSCursor *coloredIBeamCursor=[[NSCursor alloc] initWithImage:newImage hotSpot:[[NSCursor IBeamCursor] hotSpot]];
    [self resetCursorRects];
}

My final code. This takes the standard IBeam cursor (that one when you hover over a textview) and stores the colored cursor in the coloredIBeamCursor pointer.

- (void)setPointerColor:(NSColor *)newColor {
    // create the new cursor image
    [[NSGraphicsContext currentContext] CIContext];
    // create the layer with the same color as the text
    CIFilter *backgroundGenerator=[CIFilter filterWithName:@"CIConstantColorGenerator"];
    CIColor *color=[[[CIColor alloc] initWithColor:newColor] autorelease];
    [backgroundGenerator setValue:color forKey:@"inputColor"];
    CIImage *backgroundImage=[backgroundGenerator valueForKey:@"outputImage"];
    // create the cursor image
    CIImage *cursor=[CIImage imageWithData:[[[NSCursor IBeamCursor] image] TIFFRepresentation]];
    CIFilter *filter=[CIFilter filterWithName:@"CIColorInvert"];
    [filter setValue:cursor forKey:@"inputImage"];
    CIImage *outputImage=[filter valueForKey:@"outputImage"];
    // apply a multiply filter
    filter=[CIFilter filterWithName:@"CIMultiplyCompositing"];
    [filter setValue:backgroundImage forKey:@"inputImage"];
    [filter setValue:outputImage forKey:@"inputBackgroundImage"];
    outputImage=[filter valueForKey:@"outputImage"];
    // get the NSImage from the CIImage
    NSCIImageRep *rep=[NSCIImageRep imageRepWithCIImage:outputImage];
    NSImage *newImage=[[[NSImage alloc] initWithSize:[outputImage extent].size] autorelease];
    [newImage addRepresentation:rep];
    // remove the old cursor (if any)
    if (coloredIBeamCursor!=nil) {
        [self removeCursorRect:[self visibleRect] cursor:coloredIBeamCursor];
        [coloredIBeamCursor release];
    }
    // set the new cursor
    NSCursor *coloredIBeamCursor=[[NSCursor alloc] initWithImage:newImage hotSpot:[[NSCursor IBeamCursor] hotSpot]];
    [self resetCursorRects];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文