开发者

Programmatically edit image in 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 programmatical开发者_StackOverflow中文版ly edit images in Cocoa, so any help is appreciated!


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.


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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜