Created
April 14, 2014 06:39
-
-
Save Rm1210/10621763 to your computer and use it in GitHub Desktop.
rotate a NSImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Cocoa/Cocoa.h> | |
@interface NSImage (Rotated) | |
- (NSImage *)imageRotated:(float)degrees; | |
- (CGImageRef)toCGImageRef; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "NSImage+Rotated.h" | |
@implementation NSImage (Rotated) | |
- (NSImage *)imageRotated:(float)degrees { | |
degrees = fmod(degrees, 360.); | |
if (0 == degrees) { | |
return self; | |
} | |
NSSize size = [self size]; | |
NSSize maxSize; | |
if (90. == degrees || 270. == degrees || -90. == degrees || -270. == degrees) { | |
maxSize = NSMakeSize(size.height, size.width); | |
} else if (180. == degrees || -180. == degrees) { | |
maxSize = size; | |
} else { | |
maxSize = NSMakeSize(20+MAX(size.width, size.height), 20+MAX(size.width, size.height)); | |
} | |
NSAffineTransform *rot = [NSAffineTransform transform]; | |
[rot rotateByDegrees:degrees]; | |
NSAffineTransform *center = [NSAffineTransform transform]; | |
[center translateXBy:maxSize.width / 2. yBy:maxSize.height / 2.]; | |
[rot appendTransform:center]; | |
NSImage *image = [[NSImage alloc] initWithSize:maxSize]; | |
[image lockFocus]; | |
[rot concat]; | |
NSRect rect = NSMakeRect(0, 0, size.width, size.height); | |
NSPoint corner = NSMakePoint(-size.width / 2., -size.height / 2.); | |
[self drawAtPoint:corner fromRect:rect operation:NSCompositeCopy fraction:1.0]; | |
[image unlockFocus]; | |
return image; | |
} | |
- (CGImageRef)toCGImageRef | |
{ | |
// NSData *imageData = [self TIFFRepresentation]; | |
NSBitmapImageRep *bmprep = [self bitmapImageRepresentation]; | |
NSData *imageData = [bmprep representationUsingType:NSPNGFileType properties:@{NSImageCompressionFactor: @(0.0)}]; | |
CGImageRef imageRef = NULL; | |
if (imageData) { | |
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL); | |
imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL); | |
} | |
return imageRef; | |
} | |
- (NSBitmapImageRep *)bitmapImageRepresentation { | |
int width = [self size].width; | |
int height = [self size].height; | |
if(width < 1 || height < 1) | |
return nil; | |
NSBitmapImageRep *rep = [[NSBitmapImageRep alloc] | |
initWithBitmapDataPlanes: NULL | |
pixelsWide: width | |
pixelsHigh: height | |
bitsPerSample: 8 | |
samplesPerPixel: 4 | |
hasAlpha: YES | |
isPlanar: NO | |
colorSpaceName: NSDeviceRGBColorSpace | |
bytesPerRow: width * 4 | |
bitsPerPixel: 32]; | |
NSGraphicsContext *ctx = [NSGraphicsContext graphicsContextWithBitmapImageRep: rep]; | |
[NSGraphicsContext saveGraphicsState]; | |
[NSGraphicsContext setCurrentContext: ctx]; | |
[self drawAtPoint: NSZeroPoint fromRect: NSZeroRect operation: NSCompositeCopy fraction: 1.0]; | |
[ctx flushGraphics]; | |
[NSGraphicsContext restoreGraphicsState]; | |
return [rep autorelease]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment