Created
May 18, 2015 07:50
-
-
Save nvkiet/b6a7a56ec4093605b864 to your computer and use it in GitHub Desktop.
createPDFfromUIView
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
- (void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename | |
{ | |
// Creates a mutable data object for updating with binary data, like a byte array | |
NSMutableData *pdfData = [NSMutableData data]; | |
// Points the pdf converter to the mutable data object and to the UIView to be converted | |
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil); | |
UIGraphicsBeginPDFPage(); | |
CGContextRef pdfContext = UIGraphicsGetCurrentContext(); | |
// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData | |
[aView.layer renderInContext:pdfContext]; | |
// remove PDF rendering context | |
UIGraphicsEndPDFContext(); | |
// Retrieves the document directories from the iOS device | |
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES); | |
NSString* documentDirectory = [documentDirectories objectAtIndex:0]; | |
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename]; | |
// instructs the mutable data object to write its context to a file on disk | |
[pdfData writeToFile:documentDirectoryFilename atomically:YES]; | |
NSLog(@"documentDirectoryFileName: %@",documentDirectoryFilename); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment