Skip to content

Instantly share code, notes, and snippets.

@kgn
Created December 12, 2011 06:35
Show Gist options
  • Save kgn/1465424 to your computer and use it in GitHub Desktop.
Save kgn/1465424 to your computer and use it in GitHub Desktop.
Drawing an NSDrawNinePartImage from a single NSImage
static NSImage *croppedImageWithRect(NSImage *image, NSRect rect){
NSImage *subImage = [[NSImage alloc] initWithSize:rect.size];
NSRect drawRect = NSZeroRect;
drawRect.size = rect.size;
[subImage lockFocus];
[image drawInRect:drawRect
fromRect:rect
operation:NSCompositeSourceOver
fraction:1.0f];
[subImage unlockFocus];
return subImage;
}
- (void)drawRect:(NSRect)dirtyRect{
NSSize cornerSize = NSMakeSize(8.0f, 8.0f);
NSImage *wholeImage = [NSImage imageNamed:@"input"];
static NSImage *topLeftCorner = nil;
if(topLeftCorner == nil){
NSRect imageRect = NSZeroRect;
imageRect.size = cornerSize;
imageRect.origin.y = wholeImage.size.height-cornerSize.height;
topLeftCorner = croppedImageWithRect(wholeImage, imageRect);
}
static NSImage *topEdgeFill = nil;
if(topEdgeFill == nil){
NSRect imageRect = NSZeroRect;
imageRect.size.width = wholeImage.size.width-cornerSize.width*2.0f;
imageRect.size.height = cornerSize.height;
imageRect.origin.x = cornerSize.width;
imageRect.origin.y = wholeImage.size.height-cornerSize.height;
topEdgeFill = croppedImageWithRect(wholeImage, imageRect);
}
static NSImage *topRightCorner = nil;
if(topRightCorner == nil){
NSRect imageRect = NSZeroRect;
imageRect.size = cornerSize;
imageRect.origin.x = wholeImage.size.width-cornerSize.width;
imageRect.origin.y = wholeImage.size.height-cornerSize.height;
topRightCorner = croppedImageWithRect(wholeImage, imageRect);
}
static NSImage *leftEdgeFill = nil;
if(leftEdgeFill == nil){
NSRect imageRect = NSZeroRect;
imageRect.size.width = cornerSize.width;
imageRect.size.height = wholeImage.size.height-cornerSize.height*2.0f;
imageRect.origin.y = cornerSize.height;
leftEdgeFill = croppedImageWithRect(wholeImage, imageRect);
}
static NSImage *centerFill = nil;
if(centerFill == nil){
NSRect imageRect = NSZeroRect;
imageRect.size.width = wholeImage.size.width-cornerSize.width*2.0f;
imageRect.size.height = wholeImage.size.height-cornerSize.height*2.0f;
imageRect.origin.x = cornerSize.width;
imageRect.origin.y = cornerSize.height;
centerFill = croppedImageWithRect(wholeImage, imageRect);
}
static NSImage *rightEdgeFill = nil;
if(rightEdgeFill == nil){
NSRect imageRect = NSZeroRect;
imageRect.size.width = cornerSize.width;
imageRect.size.height = wholeImage.size.height-cornerSize.height*2.0f;
imageRect.origin.y = cornerSize.height;
imageRect.origin.x = wholeImage.size.width-cornerSize.width;
rightEdgeFill = croppedImageWithRect(wholeImage, imageRect);
}
static NSImage *bottomLeftCorner = nil;
if(bottomLeftCorner == nil){
NSRect imageRect = NSZeroRect;
imageRect.size = cornerSize;
bottomLeftCorner = croppedImageWithRect(wholeImage, imageRect);
}
static NSImage *bottomEdgeFill = nil;
if(bottomEdgeFill == nil){
NSRect imageRect = NSZeroRect;
imageRect.size.width = wholeImage.size.width-cornerSize.width*2.0f;
imageRect.size.height = cornerSize.height;
imageRect.origin.x = cornerSize.width;
bottomEdgeFill = croppedImageWithRect(wholeImage, imageRect);
}
static NSImage *bottomRightCorner = nil;
if(bottomRightCorner == nil){
NSRect imageRect = NSZeroRect;
imageRect.size = cornerSize;
imageRect.origin.x = wholeImage.size.width-cornerSize.width;
bottomRightCorner = croppedImageWithRect(wholeImage, imageRect);
}
NSDrawNinePartImage(self.bounds, topLeftCorner, topEdgeFill, topRightCorner,
leftEdgeFill, centerFill, rightEdgeFill, bottomLeftCorner, bottomEdgeFill,
bottomRightCorner, NSCompositeSourceOver, 1.0f, [self isFlipped]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment