1

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

Jonathan Sterling
  • 18,320
  • 12
  • 67
  • 79
TechBee
  • 1,897
  • 4
  • 22
  • 46

2 Answers2

1

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

Community
  • 1
  • 1
Luke Mcneice
  • 3,012
  • 4
  • 38
  • 50
  • Thanks Luke. I have seen this. – TechBee Feb 16 '11 at 08:38
  • There are a few different recommendations on that thread, in fact it tackles this very issue....and judging by your comments to justin you didn't actually read it. For a start, I would advise that you use `CATiledLayer` with its drawing method: `- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx` – Luke Mcneice Feb 16 '11 at 11:44
1

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.

justin
  • 104,054
  • 14
  • 179
  • 226
  • Thanks Justin. Could u please guide me how to use 200 long pages pdf with UIScrollView. What I am doing is, on Each swipe I am adding a PDF page to UIScrollView. As you mentioned drawRect could be costly, in that case what should I do? – TechBee Feb 16 '11 at 04:30
  • @Ranjan drawRect will not request an area for all 200 pages. if it does, then restrict the zooming to a usable range. draw only what is requested and visible, then see whether it needs improvement. – justin Feb 16 '11 at 05:25
  • Thanks. Let me explain you further. I am not attempting to add 200 pages when the application launches. What I am doing is,adding a single page of the pdf document to the subview of UIScrollView. And draw a PDF page, I need a context, which I am taking from drawRect Method. What could be the alternative of drawRect? drawing it in Layer? – TechBee Feb 16 '11 at 06:33
  • @Ranjan you can draw during drawRect (as you're doing in the OP). my point was that you should draw only the area you must draw, when it's requested. if you do that and take the other steps i outlined, the pdf will likely draw without issue (under normal use). addressing the list of issues should dramatically reduce the time it takes to draw. if (afterwards) it still needs improvement, then update the question with more/updated code, or ask how to improve your drawing in a new question (since you will have a different set of bottlenecks at that time). – justin Feb 16 '11 at 07:01
  • Thanks again. I got ur point,hope now I can do this. What is ur id on facebook? Would love to be connected with u to explore more on this. can u pls mail me ur facebook id. my email id is ranjanchandradey@gmail.com. – TechBee Feb 16 '11 at 07:35