Created
September 28, 2012 16:49
-
-
Save C4Code/3800901 to your computer and use it in GitHub Desktop.
Draw C4Shape to PDF
This file contains hidden or 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
// | |
// C4WorkSpace.m | |
// drawShapesToImage | |
// | |
// Created by moi on 12-09-27. | |
// Copyright (c) 2012 moi. All rights reserved. | |
// | |
#import "C4WorkSpace.h" | |
@implementation C4WorkSpace { | |
C4Shape *s; | |
CGContextRef context; | |
} | |
-(void)setup { | |
CGPoint pts[2] = {CGPointZero,CGPointMake(100, 100)}; | |
s = [C4Shape line:pts]; | |
[self.canvas addShape:s]; | |
[self runMethod:@"createAndDrawToPDF" afterDelay:0.1]; | |
} | |
-(void)touchesBegan { | |
[self createAndDrawToPDF]; | |
} | |
-(void)createAndDrawToPDF { | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *documentsDirectory = [paths objectAtIndex:0]; | |
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"tmp.pdf"]; | |
CGContextRef pdfContext = [self createPDFContext:self.canvas.frame path:(CFStringRef)writableDBPath]; | |
C4Log(@"PDF Context created"); | |
CGContextBeginPage (pdfContext,nil); | |
//turn PDF upsidedown | |
CGAffineTransform transform = CGAffineTransformIdentity; | |
transform = CGAffineTransformMakeTranslation(0, self.canvas.frame.size.height); | |
transform = CGAffineTransformScale(transform, 1.0, -1.0); | |
CGContextConcatCTM(pdfContext, transform); | |
//Draw view into PDF | |
for(C4Shape *c in self.canvas.subviews) { | |
for(int i = 0; i < 10; i++) { | |
CGPoint orig = c.frame.origin; | |
orig.y += i*10; | |
CGContextTranslateCTM(pdfContext, orig.x, orig.y); | |
[c.layer renderInContext:pdfContext]; | |
CGContextTranslateCTM(pdfContext, -1*orig.x, -1*orig.y); | |
} | |
} | |
CGContextEndPage (pdfContext); | |
CGContextRelease (pdfContext); | |
} | |
-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{ | |
CGContextRef myOutContext = NULL; | |
CFURLRef url; | |
url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, false); | |
if (url != NULL) { | |
myOutContext = CGPDFContextCreateWithURL (url, &inMediaBox, NULL); | |
CFRelease(url); | |
} | |
return myOutContext; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment