Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSImage colorWithPatternImage: how to tile images from the top instead?

The NSImage colorWithPatternImage method tiles images from the bottom. Can can I tiles them from the top instead ?

+ (NSColor *)colorWithPatternImage:(NSImage *)image Parameters image

The image to use as the pattern for the color object. The image is tiled starting at the bottom of the window. The image is not scaled.

This is my code in my subclass of IKImageBrowserView, which is the view inside NSScrollView in my app.

- (void)drawRect:(NSRect)rect
{
    NSImage * backgroundImage = [NSImage imageNamed:@"deliciousLibrary.png"];
    [backgroundImage setSize:NSMakeSize(459 * bgImageSize, 91 * bgImageSize)];
    [[NSColor colorWithPatternImage:backgroundImage] set];
    NSRectFill(rect);
    [super drawRect:rect];
}

thanks

UPDATE: I've found on the internet this solution. But in my case it doesn't work because the image is not scrolling anymore in my NSScrollView.

- (void)drawRect:(NSRect)dirtyRect {
    NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
    [theContext saveGraphicsState];
    [[NSGraphicsContext currentContext] setPatternPhase:NSMakePoint(0,[self frame].size.height)];
    [self.customBackgroundColour set];
    NSRectFill([self bounds]);
    [theContext restoreGraphicsState]; 
}
like image 573
aneuryzm Avatar asked Nov 26 '25 19:11

aneuryzm


1 Answers

This is the solution to my problem. Amazingly simple.

float yOffset = NSMaxY([self convertRect:[self frame] toView:nil]);
[[NSGraphicsContext currentContext] setPatternPhase:NSMakePoint(0,yOffset)];
like image 66
aneuryzm Avatar answered Nov 28 '25 09:11

aneuryzm