Rotate NSImage in an NSImageView
I didn’t want to half-implement the new tab feature in Slider – I want people to have almost as much control as if they were coding the site by hand.
This has meant I’ve had to rethink Slider’s UI many times, and I’ve had (re: couldn’t face shipping without) to implement many small features that I feel are required, from a usability standpoint.
When I decided that the user may choose to place tabs in 12 different positions, I needed to consider how this would effect the arrows and sliding animations. I came to the conclusion that if the user elects to place the tabs on the top or bottom, the arrows will be on the left and right, with the animation going right/left. If the user chooses to place tabs on the left or right, the arrows will be positioned on the top and bottom, with the animation going up/down.
Pictures make it easier:


As soon as I implemented this I realised how much of a pain it would be to copy out the arrow images, rotate them in an external program, then copy them back in. So I put some buttons next to the arrow image wells that allow the user to rotate the arrows within Slider itself.
Here is the code I used to rotate the images (thanks to Jerry Krinock and Steve Christensen and for posting this solution to the Cocoa-dev list):
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | #import <Cocoa/Cocoa.h> @interface NSImage (Transform) /*! @brief Rotates an image around its center by a given angle in degrees and returns the new image. @details The width and height of the returned image are, respectively, the height and width of the receiver. I have not yet tested this with a non-square image. authors: Jerry Krinock and Steve Christensen */ - (NSImage*)imageRotatedByDegrees:(CGFloat)degrees ; @end #import "NSImage+Transform.h" @implementation NSImage (Transform) - (NSImage*)imageRotatedByDegrees:(CGFloat)degrees { // calculate the bounds for the rotated image NSRect imageBounds = {NSZeroPoint, [self size]}; NSBezierPath* boundsPath = [NSBezierPath bezierPathWithRect:imageBounds]; NSAffineTransform* transform = [NSAffineTransform transform]; [transform rotateByDegrees:degrees]; [boundsPath transformUsingAffineTransform:transform]; NSRect rotatedBounds = {NSZeroPoint, [boundsPath bounds].size}; NSImage* rotatedImage = [[NSImage alloc] initWithSize:rotatedBounds.size]; // center the image within the rotated bounds imageBounds.origin.x = NSMidX(rotatedBounds) - (NSWidth (imageBounds) / 2); imageBounds.origin.y = NSMidY(rotatedBounds) - (NSHeight (imageBounds) / 2); // set up the rotation transform transform = [NSAffineTransform transform]; [transform translateXBy:+(NSWidth(rotatedBounds) / 2) yBy:+ (NSHeight(rotatedBounds) / 2)]; [transform rotateByDegrees:degrees]; [transform translateXBy:-(NSWidth(rotatedBounds) / 2) yBy:- (NSHeight(rotatedBounds) / 2)]; // draw the original image, rotated, into the new image [rotatedImage lockFocus]; [transform concat]; [self drawInRect:imageBounds fromRect:NSZeroRect operation:NSCompositeCopy fraction:1.0] ; [rotatedImage unlockFocus]; return [rotatedImage autorelease]; } @end |
I had to change (from/to):
NSImage* rotatedImage = [[[NSImage alloc] initWithSize:rotatedBounds.size] autorelease];
NSImage* rotatedImage = [[NSImage alloc] initWithSize:rotatedBounds.size];
return rotatedImage;return [rotatedImage autorelease];
[transform set];
[transform concat];
For it to work flawlessly with Slider.
Thanks guys!
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
The Best Photographer In Shanghai
When we pick them up :( Don't know when that will be yet!
Chose another 49 today
Wed, 13 Jan 2010 14:13:21 +0000
The Best Photographer In Shanghai
When do we get to see them!!!!!
Tue, 12 Jan 2010 19:08:51 +0000
Installing Jdownloader In Ubuntu
Thank You!!!!
It works like a charm!!!!!!!!!!!!
Tue, 05 Jan 2010 13:02:35 +0000
Arrived In Shanghai
Done, look left!
Sun, 03 Jan 2010 15:37:03 +0000
Arrived In Shanghai
Hi :)
I'll see what I can do!
Sun, 03 Jan 2010 14:23:07 +0000
Arrived In Shanghai
Hi Mike. It would be very useful to have the time date and weather conditions in Shanghai, on your site. Be seeing you soon. Love NZMum.
Sat, 02 Jan 2010 23:30:41 +0000