Question

I am drawing on a custom view an NSGradient like this:

- (void)drawRect: (NSRect)dirtyRect
{
    NSGradient* g = [[NSGradient alloc] initWithStartingColor: _color endingColor: [NSColor clearColor]];   
    [g drawInRect: [self bounds] angle: 90];
}

If _color is a normal color, for example [NSColor blueColor], everything is fine. The problem is that _color comes from a pattern image (which in this case is mostly grey with some fancy pixels on it), and when that happens the program keeps logging this error:

*** -[NSGradient initWithColors:atLocations:colorSpace:]: the color NSPatternColorSpace CGImageSource=0x400367d60" )> cannot be converted into color space Generic RGB colorspace

_color = [NSColor colorWithPatternImage: [NSImage imageNamed: @"mainBG.png"]]

The image is completely opaque and is a png file. Any ideas? perhaps I should change the file type? I don't know...

EDIT:

If I define _color like this:

_color = [[NSColor colorWithPatternImage: [NSImage imageNamed: @"mainBG.tiff"]] colorUsingColorSpace: [NSColorSpace genericRGBColorSpace]]

then no gradient is displayed. Nothing. Just as if I didn't have the drawRect: method. Any ideas?

Was it helpful?

Solution

An NSGradient doesn't work with a pattern as one of the colors. That's what the exception is telling you. It will only work with an NSColor that can be converted to an RGB color. If you want to make your pattern fade to clear, you'll have to do it another way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top