开发者

Converting UIImage into PDF File

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 solut开发者_StackOverflowion for the issue i faced.

Thank You.


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


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.


I got a blank pdf as well. Got it working now though. Try changing:

//[aView drawRect:aView.bounds]; // <- This

[aView.layer renderInContext:UIGraphicsGetCurrentContext()]; // <- To This


You can start a pdf graphics context, and then draw an image into it, using:

[UIImage drawInRect: someRect];

You can either see the docs, they give a good explanation of generating a pdf. There is a good tutorial on pdf generation here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜