2

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?

Alex
  • 5,009
  • 3
  • 39
  • 73
  • what about saving the PNG file in the RGB colorspace and then retry the same code ? –  Dec 30 '11 at 12:44
  • @Vince In the info window in Preview it says that the image has the `ColorSync Profile sRGB IEC61966-2.1`. Since `sRGB IEC..` appears in the error as the ColorSpace, I'm assuming you mean I have to change that. How do I do it? – Alex Dec 30 '11 at 15:36
  • you could try using drawing software –  Dec 30 '11 at 16:05
  • @Vince yes, I tried using Photoshop and converting from sRGB to Generic RGB Profile and it didn't work. Exactly the same error message. What is weird is that, while the NSImage description stated that its `ColorSpace` was `Generic RGB Profile`, the error still said that the color couldn't be converted into the color space `Generic RGB colorspace` – Alex Dec 30 '11 at 16:37
  • @Vince btw, I also tried .jpg and .tiff and got nothing – Alex Dec 30 '11 at 16:38

1 Answers1

1

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.

Ken Aspeslagh
  • 11,484
  • 2
  • 36
  • 42
  • 1
    Probably the best way to go about this would be to draw the texture onto the view in question, then draw the gradient on top of that texture with the appropriate blending modes. – sudo rm -rf Apr 10 '12 at 23:32