TechBee
TechBee

Reputation: 1941

Rendering PDF document having 200 pages on iPad

I have to develop an application which takes PDF as input, that PDF document has 200 pages. I am using UIScrollView to swipe left and right. On each swipe I am drawing a PDF document. The code is as under :

- (id)initWithFrame:(CGRect)frame content:(NSString *)aPDFPage type:(NSString *)contentType
{
    if ((self = [super initWithFrame:frame])) 
    {
        // Initialization code
        self.backgroundColor = [UIColor whiteColor];
        pageRef = [[NSString alloc]initWithString:aPDFPage];
        pageTypeRef = [[NSString alloc]initWithString:contentType];
    }
    return self;
}


- (void)drawRect:(CGRect)rect 
{
    ctx = UIGraphicsGetCurrentContext();
    [self drawPDF];
}

-(void)drawPDF
{
    NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:self.pageRef ofType:self.pageTypeRef];
    NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];

    document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);

    page = CGPDFDocumentGetPage(document, 1);
    CGContextTranslateCTM(ctx, 0.0, [self bounds].size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);

    CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFTrimBox),CGContextGetClipBoundingBox(ctx));

    CGContextConcatCTM(ctx, transform);

    CGContextSetInterpolationQuality(ctx, kCGInterpolationLow); 
    CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);

    CGContextDrawPDFPage(ctx, page);
    CGPDFDocumentRelease(document);
}


-(void)dealloc 
{
    [pageRef release];
    [pageTypeRef release];
    [super dealloc];
}

This works fine. But if I swipe very fast, more often subsequent pages does not load instantly. Screen becomes white.

How to solve this, please guide.

Regards, Ranjan

Upvotes: 1

Views: 1806

Answers (2)

justin
justin

Reputation: 104698

one of the obvious issues is that drawRect may be called regularly, and may only request that you draw some of the view's rect.

in your implementation, you:

  • read the pdf from disk. this takes a lot of time, especially since drawRect may be called at a high frequency.

  • read the pdf. this takes some time -- it can be avoided from occurring during drawRect.

  • draw one page. draw only what you need to draw, where possible.

  • dispose of the pdf. you should hold on to the pdf document while the pdf view is visible, rather than reading it from disk every time you need to draw.

Upvotes: 1

Luke Mcneice
Luke Mcneice

Reputation: 2720

Have a look at this thread: Fast and Lean PDF Viewer for iPhone / iPad / iOs - tips and hints?

Upvotes: 1

Related Questions