Question

I've having trouble to understand that following piece of code :

[window setBackgroundColor:[NSColor colorWithPatternImage:[NSImage imageNamed:@"myImage.png"]]];

It's suppose to set a background image to a NSWindow. But

  1. Why setBackgroundColor ?
  2. What does NSColor colorWithPatternImage really do ? Should a NSColor only be a color ?

My question a probably simple but it's disturbing me.

Thx

Was it helpful?

Solution

Core Graphics has (similar to PostScript and PDF), the concept of a "pattern color". See for example Patterns in the "Quartz 2D Programming Guide":

A pattern is a sequence of drawing operations that is repeatedly painted to a graphics context. You can use patterns in the same way as you use colors.

[NSColor colorWithPatternImage:image] creates a pattern that draws the image repeatedly. So in your case, if the image has the same size as the window, it is drawn just once. But if the image is smaller than the window, it will be repeatedly drawn in x- and y-direction to fill the window background.

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