Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Moved TUIGraphics* functions into TUICGAdditions, so code doesn't hav…

…e to import TUIKit.h everywhere
  • Loading branch information...
commit 0163077fc31d081e6c73db4d88c80d1d42e6848d 1 parent f7daaf2
@jspahrsummers jspahrsummers authored
View
25 lib/UIKit/TUICGAdditions.h
@@ -29,6 +29,9 @@ typedef NSUInteger TUICGRoundedRectCorner;
#import <Foundation/Foundation.h>
+@class TUIImage;
+@class TUIView;
+
extern CGContextRef TUICreateOpaqueGraphicsContext(CGSize size);
extern CGContextRef TUICreateGraphicsContext(CGSize size);
extern CGContextRef TUICreateGraphicsContextWithOptions(CGSize size, BOOL opaque);
@@ -47,3 +50,25 @@ extern CGRect ABIntegralRectWithSizeCenteredInRect(CGSize s, CGRect r);
extern void CGContextFillRoundRect(CGContextRef context, CGRect rect, CGFloat radius);
extern void CGContextDrawLinearGradientBetweenPoints(CGContextRef context, CGPoint a, CGFloat color_a[4], CGPoint b, CGFloat color_b[4]);
+
+extern CGContextRef TUIGraphicsGetCurrentContext(void);
+extern void TUIGraphicsPushContext(CGContextRef context);
+extern void TUIGraphicsPopContext(void);
+
+extern TUIImage *TUIGraphicsContextGetImage(CGContextRef ctx);
+
+extern void TUIGraphicsBeginImageContext(CGSize size);
+extern void TUIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
+extern TUIImage *TUIGraphicsGetImageFromCurrentImageContext(void);
+extern void TUIGraphicsEndImageContext(void);
+
+extern TUIImage *TUIGraphicsGetImageForView(TUIView *view);
+
+extern TUIImage *TUIGraphicsDrawAsImage(CGSize size, void(^draw)(void));
+
+/**
+ Draw drawing as a PDF
+ @param optionalMediaBox may be NULL
+ @returns NSData encapsulating the PDF drawing, suitable for writing to a file or the pasteboard
+ */
+extern NSData *TUIGraphicsDrawAsPDF(CGRect *optionalMediaBox, void(^draw)(CGContextRef));
View
88 lib/UIKit/TUICGAdditions.m
@@ -15,6 +15,8 @@
*/
#import "TUICGAdditions.h"
+#import "TUIImage.h"
+#import "TUIView.h"
CGContextRef TUICreateOpaqueGraphicsContext(CGSize size)
{
@@ -174,3 +176,89 @@ void CGContextDrawLinearGradientBetweenPoints(CGContextRef context, CGPoint a, C
CGColorSpaceRelease(colorspace);
CGGradientRelease(gradient);
}
+
+CGContextRef TUIGraphicsGetCurrentContext(void)
+{
+ return (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
+}
+
+void TUIGraphicsPushContext(CGContextRef context)
+{
+ NSGraphicsContext *c = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
+ [NSGraphicsContext saveGraphicsState];
+ [NSGraphicsContext setCurrentContext:c];
+}
+
+void TUIGraphicsPopContext(void)
+{
+ [NSGraphicsContext restoreGraphicsState];
+}
+
+TUIImage* TUIGraphicsContextGetImage(CGContextRef ctx)
+{
+ CGImageRef CGImage = TUICreateCGImageFromBitmapContext(ctx);
+ TUIImage *image = [TUIImage imageWithCGImage:CGImage];
+ CGImageRelease(CGImage);
+
+ return image;
+}
+
+void TUIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
+{
+ size.width *= scale;
+ size.height *= scale;
+ if(size.width < 1) size.width = 1;
+ if(size.height < 1) size.height = 1;
+ CGContextRef ctx = TUICreateGraphicsContextWithOptions(size, opaque);
+ TUIGraphicsPushContext(ctx);
+ CGContextRelease(ctx);
+}
+
+void TUIGraphicsBeginImageContext(CGSize size)
+{
+ TUIGraphicsBeginImageContextWithOptions(size, NO, 1.0f);
+}
+
+TUIImage* TUIGraphicsGetImageFromCurrentImageContext(void)
+{
+ return TUIGraphicsContextGetImage(TUIGraphicsGetCurrentContext());
+}
+
+TUIImage* TUIGraphicsGetImageForView(TUIView *view)
+{
+ TUIGraphicsBeginImageContext(view.frame.size);
+ [view.layer renderInContext:TUIGraphicsGetCurrentContext()];
+ TUIImage *image = TUIGraphicsGetImageFromCurrentImageContext();
+ TUIGraphicsEndImageContext();
+ return image;
+}
+
+void TUIGraphicsEndImageContext(void)
+{
+ TUIGraphicsPopContext();
+}
+
+TUIImage *TUIGraphicsDrawAsImage(CGSize size, void(^draw)(void))
+{
+ TUIGraphicsBeginImageContext(size);
+ draw();
+ TUIImage *image = TUIGraphicsGetImageFromCurrentImageContext();
+ TUIGraphicsEndImageContext();
+ return image;
+}
+
+NSData* TUIGraphicsDrawAsPDF(CGRect *optionalMediaBox, void(^draw)(CGContextRef))
+{
+ NSMutableData *data = [NSMutableData data];
+ CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)data);
+ CGContextRef ctx = CGPDFContextCreate(dataConsumer, optionalMediaBox, NULL);
+ CGPDFContextBeginPage(ctx, NULL);
+ TUIGraphicsPushContext(ctx);
+ draw(ctx);
+ TUIGraphicsPopContext();
+ CGPDFContextEndPage(ctx);
+ CGPDFContextClose(ctx);
+ CGContextRelease(ctx);
+ CGDataConsumerRelease(dataConsumer);
+ return data;
+}
View
22 lib/UIKit/TUIKit.h
@@ -57,26 +57,4 @@
#import "TUIViewController.h"
#import "TUIViewNSViewContainer.h"
-extern CGContextRef TUIGraphicsGetCurrentContext(void);
-extern void TUIGraphicsPushContext(CGContextRef context);
-extern void TUIGraphicsPopContext(void);
-
-extern TUIImage *TUIGraphicsContextGetImage(CGContextRef ctx);
-
-extern void TUIGraphicsBeginImageContext(CGSize size);
-extern void TUIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
-extern TUIImage *TUIGraphicsGetImageFromCurrentImageContext(void);
-extern void TUIGraphicsEndImageContext(void);
-
-extern TUIImage *TUIGraphicsGetImageForView(TUIView *view);
-
-extern TUIImage *TUIGraphicsDrawAsImage(CGSize size, void(^draw)(void));
-
-/**
- Draw drawing as a PDF
- @param optionalMediaBox may be NULL
- @returns NSData encapsulating the PDF drawing, suitable for writing to a file or the pasteboard
- */
-extern NSData *TUIGraphicsDrawAsPDF(CGRect *optionalMediaBox, void(^draw)(CGContextRef));
-
extern BOOL AtLeastLion; // set at launch
View
88 lib/UIKit/TUIKit.m
@@ -14,92 +14,4 @@
limitations under the License.
*/
-#import "TUIKit.h"
-
BOOL AtLeastLion = NO;
-
-CGContextRef TUIGraphicsGetCurrentContext(void)
-{
- return (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
-}
-
-void TUIGraphicsPushContext(CGContextRef context)
-{
- NSGraphicsContext *c = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
- [NSGraphicsContext saveGraphicsState];
- [NSGraphicsContext setCurrentContext:c];
-}
-
-void TUIGraphicsPopContext(void)
-{
- [NSGraphicsContext restoreGraphicsState];
-}
-
-TUIImage* TUIGraphicsContextGetImage(CGContextRef ctx)
-{
- CGImageRef CGImage = TUICreateCGImageFromBitmapContext(ctx);
- TUIImage *image = [TUIImage imageWithCGImage:CGImage];
- CGImageRelease(CGImage);
-
- return image;
-}
-
-void TUIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale)
-{
- size.width *= scale;
- size.height *= scale;
- if(size.width < 1) size.width = 1;
- if(size.height < 1) size.height = 1;
- CGContextRef ctx = TUICreateGraphicsContextWithOptions(size, opaque);
- TUIGraphicsPushContext(ctx);
- CGContextRelease(ctx);
-}
-
-void TUIGraphicsBeginImageContext(CGSize size)
-{
- TUIGraphicsBeginImageContextWithOptions(size, NO, 1.0f);
-}
-
-TUIImage* TUIGraphicsGetImageFromCurrentImageContext(void)
-{
- return TUIGraphicsContextGetImage(TUIGraphicsGetCurrentContext());
-}
-
-TUIImage* TUIGraphicsGetImageForView(TUIView *view)
-{
- TUIGraphicsBeginImageContext(view.frame.size);
- [view.layer renderInContext:TUIGraphicsGetCurrentContext()];
- TUIImage *image = TUIGraphicsGetImageFromCurrentImageContext();
- TUIGraphicsEndImageContext();
- return image;
-}
-
-void TUIGraphicsEndImageContext(void)
-{
- TUIGraphicsPopContext();
-}
-
-TUIImage *TUIGraphicsDrawAsImage(CGSize size, void(^draw)(void))
-{
- TUIGraphicsBeginImageContext(size);
- draw();
- TUIImage *image = TUIGraphicsGetImageFromCurrentImageContext();
- TUIGraphicsEndImageContext();
- return image;
-}
-
-NSData* TUIGraphicsDrawAsPDF(CGRect *optionalMediaBox, void(^draw)(CGContextRef))
-{
- NSMutableData *data = [NSMutableData data];
- CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef)data);
- CGContextRef ctx = CGPDFContextCreate(dataConsumer, optionalMediaBox, NULL);
- CGPDFContextBeginPage(ctx, NULL);
- TUIGraphicsPushContext(ctx);
- draw(ctx);
- TUIGraphicsPopContext();
- CGPDFContextEndPage(ctx);
- CGPDFContextClose(ctx);
- CGContextRelease(ctx);
- CGDataConsumerRelease(dataConsumer);
- return data;
-}
Please sign in to comment.
Something went wrong with that request. Please try again.