Tinting a grayscale NSImage (or CIImage)

后端 未结 8 1916
轮回少年
轮回少年 2021-01-30 09:02

I have a grayscale image which I want to use for drawing Cocoa controls. The image has various levels of gray. Where it is darkest, I want it to draw a specified tint color dark

8条回答
  •  不思量自难忘°
    2021-01-30 10:06

    The above solution didn't work for me. But this much easier solution works great for me

    - (NSImage *)imageTintedWithColor:(NSColor *)tint
    {
        NSImage *image = [self copy];
        if (tint) {
            [image lockFocus];
            [tint set];
            NSRect imageRect = {NSZeroPoint, [image size]};
            NSRectFillUsingOperation(imageRect, NSCompositeSourceIn);
            [image unlockFocus];
        }
        return image;
    }
    

提交回复
热议问题