Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating PDF using UIImage #3

Open
oskargargas opened this issue Jul 17, 2013 · 1 comment
Open

Creating PDF using UIImage #3

oskargargas opened this issue Jul 17, 2013 · 1 comment

Comments

@oskargargas
Copy link

If you'd be interested in expanding MTPDF to create PDFs using UIImages I'd like to share this code as a starting point:

+ (MTPDF *)PDFWithImageArray: (NSArray /* <UIImage> */*)imageArray {
    NSMutableData *pdfData = [[NSMutableData alloc] init];
    CGDataConsumerRef pdfConsumer = CGDataConsumerCreateWithCFData((__bridge CFMutableDataRef) pdfData);

    CGContextRef pdfContext = CGPDFContextCreate(pdfConsumer, NULL, NULL);

    for (NSUInteger i = 0; i < imageArray.count; i++) {
        UIImage *image = imageArray[i];
        CGFloat pageWidth = image.size.width;
        CGFloat pageHeight = image.size.height;

        CGRect pageRect = CGRectMake(0, 0, pageWidth, pageHeight);
        CGContextBeginPage(pdfContext,&pageRect);
        CGContextDrawImage(pdfContext, pageRect, image.CGImage);
        CGContextEndPage(pdfContext);
    }

    CGContextRelease(pdfContext);
    CGDataConsumerRelease(pdfConsumer);

    return [MTPDF PDFWithData:pdfData];
}
@atomkirk
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants