Konstantin.Efimenko
Konstantin.Efimenko

Reputation: 1450

UIGraphicsImageRenderer does not see some content on PDFPage

I use code from huckingwithswift.com as it is:

private func imageFromPDF(page: CGPDFPage) -> UIImage {
    let pageRect = page.getBoxRect(.mediaBox)
    let renderer = UIGraphicsImageRenderer(size: pageRect.size)
    let image = renderer.image { ctx in
        UIColor.white.set()
        ctx.fill(pageRect)
        ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
        ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
        ctx.cgContext.drawPDFPage(page)
    }
    return image
}

, and on previous examples (QRCode in PDF) it works good. Now a test ingeneer reported that my application can not see QRCodes on such page: enter image description here

UIGraphicsImageRenderer creates an image, which looks like enter image description here You can see, it does not see this QRCodes It may be because of different layers, but how to make all content be visible?

Upvotes: 1

Views: 76

Answers (1)

Alexandr Hotko
Alexandr Hotko

Reputation: 83

Annotations are drawn only in thumbnails. Try PDFPage class. You need render big thumb, not page.

#import <PDFKit/PDFKit.h>

+ (UIImage *)image:(PDFPage *)page
{
    CGRect pageRect = [ page boundsForBox:kPDFDisplayBoxMediaBox ];
    pageRect = CGRectApplyAffineTransform(pageRect, [ page transformForBox:kPDFDisplayBoxMediaBox ]);
    UIImage * image = [ page thumbnailOfSize:pageRect.size forBox:kPDFDisplayBoxMediaBox ];

    return image;
}

Upvotes: 1

Related Questions