Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting UIImage into PDF File

Tags:

iphone

Iam trying to saving an UIImage in PDF file. How can i do this? How i would save and image into pdf file and then export that pdf file? Please suggest the solution for the issue i faced.

Thank You.

like image 576
mactalent Avatar asked Mar 15 '10 05:03

mactalent


2 Answers

Hello there I've found that this works, hope it helps!

-(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();

        // draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData
        [aView.layer renderInContext:UIGraphicsGetCurrentContext()];

        // 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);
    }
like image 181
user440071 Avatar answered Oct 09 '22 13:10

user440071


My understanding is that you'd create a CGPDFContext, draw your UIImage into it, and save it to a file. Haven't done that myself, though.

like image 35
Thomas Müller Avatar answered Oct 09 '22 14:10

Thomas Müller