Skip to content

Creating PDF using UIImage #3

Open
@oskargargas

Description

@oskargargas

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions