Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Refactored TUICGAdditions to use NSImage
  • Loading branch information
jspahrsummers committed Jul 25, 2012
1 parent 5467b74 commit 239b3c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
9 changes: 4 additions & 5 deletions lib/UIKit/TUICGAdditions.h
Expand Up @@ -29,7 +29,6 @@ typedef NSUInteger TUICGRoundedRectCorner;


#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>


@class TUIImage;
@class TUIView; @class TUIView;


extern CGContextRef TUICreateOpaqueGraphicsContext(CGSize size); extern CGContextRef TUICreateOpaqueGraphicsContext(CGSize size);
Expand All @@ -55,16 +54,16 @@ extern CGContextRef TUIGraphicsGetCurrentContext(void);
extern void TUIGraphicsPushContext(CGContextRef context); extern void TUIGraphicsPushContext(CGContextRef context);
extern void TUIGraphicsPopContext(void); extern void TUIGraphicsPopContext(void);


extern TUIImage *TUIGraphicsContextGetImage(CGContextRef ctx); extern NSImage *TUIGraphicsContextGetImage(CGContextRef ctx);


extern void TUIGraphicsBeginImageContext(CGSize size); extern void TUIGraphicsBeginImageContext(CGSize size);
extern void TUIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale); extern void TUIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
extern TUIImage *TUIGraphicsGetImageFromCurrentImageContext(void); extern NSImage *TUIGraphicsGetImageFromCurrentImageContext(void);
extern void TUIGraphicsEndImageContext(void); extern void TUIGraphicsEndImageContext(void);


extern TUIImage *TUIGraphicsGetImageForView(TUIView *view); extern NSImage *TUIGraphicsGetImageForView(TUIView *view);


extern TUIImage *TUIGraphicsDrawAsImage(CGSize size, void(^draw)(void)); extern NSImage *TUIGraphicsDrawAsImage(CGSize size, void(^draw)(void));


/** /**
Draw drawing as a PDF Draw drawing as a PDF
Expand Down
18 changes: 10 additions & 8 deletions lib/UIKit/TUICGAdditions.m
Expand Up @@ -15,7 +15,6 @@
*/ */


#import "TUICGAdditions.h" #import "TUICGAdditions.h"
#import "TUIImage.h"
#import "TUIView.h" #import "TUIView.h"


CGContextRef TUICreateOpaqueGraphicsContext(CGSize size) CGContextRef TUICreateOpaqueGraphicsContext(CGSize size)
Expand Down Expand Up @@ -194,10 +193,11 @@ void TUIGraphicsPopContext(void)
[NSGraphicsContext restoreGraphicsState]; [NSGraphicsContext restoreGraphicsState];
} }


TUIImage* TUIGraphicsContextGetImage(CGContextRef ctx) NSImage *TUIGraphicsContextGetImage(CGContextRef ctx)
{ {
CGImageRef CGImage = TUICreateCGImageFromBitmapContext(ctx); CGImageRef CGImage = TUICreateCGImageFromBitmapContext(ctx);
TUIImage *image = [TUIImage imageWithCGImage:CGImage]; CGSize size = CGSizeMake(CGImageGetWidth(CGImage), CGImageGetHeight(CGImage));
NSImage *image = [[NSImage alloc] initWithCGImage:CGImage size:size];
CGImageRelease(CGImage); CGImageRelease(CGImage);


return image; return image;
Expand All @@ -219,17 +219,18 @@ void TUIGraphicsBeginImageContext(CGSize size)
TUIGraphicsBeginImageContextWithOptions(size, NO, 1.0f); TUIGraphicsBeginImageContextWithOptions(size, NO, 1.0f);
} }


TUIImage* TUIGraphicsGetImageFromCurrentImageContext(void) NSImage *TUIGraphicsGetImageFromCurrentImageContext(void)
{ {
return TUIGraphicsContextGetImage(TUIGraphicsGetCurrentContext()); return TUIGraphicsContextGetImage(TUIGraphicsGetCurrentContext());
} }


TUIImage* TUIGraphicsGetImageForView(TUIView *view) NSImage *TUIGraphicsGetImageForView(TUIView *view)
{ {
TUIGraphicsBeginImageContext(view.frame.size); TUIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:TUIGraphicsGetCurrentContext()]; [view.layer renderInContext:TUIGraphicsGetCurrentContext()];
TUIImage *image = TUIGraphicsGetImageFromCurrentImageContext(); NSImage *image = TUIGraphicsGetImageFromCurrentImageContext();
TUIGraphicsEndImageContext(); TUIGraphicsEndImageContext();

return image; return image;
} }


Expand All @@ -238,12 +239,13 @@ void TUIGraphicsEndImageContext(void)
TUIGraphicsPopContext(); TUIGraphicsPopContext();
} }


TUIImage *TUIGraphicsDrawAsImage(CGSize size, void(^draw)(void)) NSImage *TUIGraphicsDrawAsImage(CGSize size, void(^draw)(void))
{ {
TUIGraphicsBeginImageContext(size); TUIGraphicsBeginImageContext(size);
draw(); draw();
TUIImage *image = TUIGraphicsGetImageFromCurrentImageContext(); NSImage *image = TUIGraphicsGetImageFromCurrentImageContext();
TUIGraphicsEndImageContext(); TUIGraphicsEndImageContext();

return image; return image;
} }


Expand Down

0 comments on commit 239b3c9

Please sign in to comment.