Tile an NSImage Within an NSBezierPath
Another snippet I had to write during the great Slider rebuild.
I needed to be able to take an NSImage supplied by the user and tile it within an NSBezierPath defined by more user-set values.
The below example uses a rounded rectangle.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | NSBitmapImageRep *theImage = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:imageSize.width pixelsHigh:imageSize.height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0 bitsPerPixel:0]; [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:theImage]]; //bgImage can be any NSImage NSColor *c = [NSColor colorWithPatternImage:bgImage]; [c set]; [path appendBezierPathWithRoundedRect:NSMakeRect(0, 0, imageSize.width, imageSize.height) xRadius:10.0 yRadius:10.0]; [path fill]; [NSGraphicsContext restoreGraphicsState]; |
Like this post? Move it on along with:
Email |
delicious |
Digg |
Tweet |
Reddit |
Newsvine |
Furl |
Google |
Stumble |
HaoHao
| Trackback: |
Scroll to post title
Fill a Path With A Scaled Image, Ignoring Image Proportions
While continuing to rebuild Slider’s background image features, I was forced to rewrite the code that scales an image up/down to Slider’s portal size, ignoring the image’s original proportions. The image also had to be painted within a path, as Slider may have differently shaped corners compared to the image to be scaled.
The below code accomplishes the scaling and painting of the image.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | //theImage = the resulting image, that will have a scaled (proportions ignored) version of "bgImage" painted onto it NSBitmapImageRep *theImage = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL pixelsWide:imageSize.width pixelsHigh:imageSize.height bitsPerSample:8 samplesPerPixel:4 hasAlpha:YES isPlanar:NO colorSpaceName:NSDeviceRGBColorSpace bytesPerRow:0 bitsPerPixel:0]; [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:theImage]]; NSImage bg = [[NSImage alloc] initWithSize: imageSize]; //bgImage can be any NSImage NSSize originalSize = [bgImage size]; [bg lockFocus]; [bgImage drawInRect: NSMakeRect(0, 0, imageSize.width, imageSize.height) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0]; [bg unlockFocus]; NSColor *c = [NSColor colorWithPatternImage:bg]; [c set]; [path appendBezierPathWithRoundedRect:NSMakeRect(0, 0, imageSize.width, imageSize.height) xRadius:10.0 yRadius:10.0]; [aPath fill]; [NSGraphicsContext restoreGraphicsState]; |
“path” could be any NSBezierPath.
Like this post? Move it on along with:
Email |
delicious |
Digg |
Tweet |
Reddit |
Newsvine |
Furl |
Google |
Stumble |
HaoHao
| Trackback: |
Scroll to post title
Fancy Footer Update
I’ve updated the Fancy Footer snippet, now Trackbacks/Pingbacks will be able to find your posts – the previous version’s implementation was … lacking.
The previous snippet used the post permalink as both the permalink and the path – the Trackback/Pingback system didn’t like “http://“ being included in the path attribute.
I was afraid that I wouldn’t be able to find a solution that allowed us to have Trackback/Pingback support and keep our existing comments.
I am glad to say that my fears were unfounded, and I managed to come up with a solution. The snippet now parses out the “http://domain.com” part of the permalink, and uses that for the path attribute for the widgets. Happily, JS-Kit must do a similar thing, as it hasn’t messed up my comments (the path attribute is what JS-Kit uses to determine what page to display which comments).
The snippet may be downloaded here. To learn how to use this snippet, read Fancy Footer – Usage and Tips.
Like this post? Move it on along with:
Email |
delicious |
Digg |
Tweet |
Reddit |
Newsvine |
Furl |
Google |
Stumble |
HaoHao
| Trackback: |
Scroll to post title
Archive Annihilator Improved Again
The solution for the ’sidebar explosion’ effect that the Archive Annihilator caused occurred to me earlier today. The fix was pretty simple, the fact that I didn’t think of it right away is a little embarrassing.
The update has been uploaded, and is available here.
Like this post? Move it on along with:
Email |
delicious |
Digg |
Tweet |
Reddit |
Newsvine |
Furl |
Google |
Stumble |
HaoHao
| Trackback: |
Scroll to post title




























Recent Comments
Js Kit Comments Correct Usage Of The Permalink And Path Attributes
演唱关于非常规划表吧
Wed, 10 Mar 2010 09:22:50 +0000
The Best Photographer In Shanghai
Yes I'm in Shanghai, no I'm not Chinese.
你在上海吗?华人吗?
Tue, 09 Mar 2010 06:32:40 +0000