Raphael
Raphael

Reputation: 8192

CGContextDrawPDFPage is drawing the page on the wrong origin point

Edit: It seems that the origin point's components are been multiplied by 2 when drawing the PDF page. Is something I'm doing causing this?

I have a UIViewController that displays a PDF page on a UIScrollView. the PDF page itself is drawn on a CATiledLayer, however, when my view controller draws the layer there is a considerable offset between the layer's bounds and the drawn PDF. As you can see in the picture, the page should have been drawn on the yellow view:

The PDF is drawn with a considerable offset from it's desired origin

Here is the relevant code that draws the page:

- (void)refreshPage
{

    if(contentView) {
        for(UIView *v in self.view.subviews) {
            [v removeFromSuperview];
            [v release];
        }
    }

    CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(self._document.page, kCGPDFCropBox));

    pageRect.origin.x = ((self.view.frame.size.width / 2) - (pageRect.size.width / 2));

    CATiledLayer *tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
    tiledLayer.levelsOfDetail = 1000;
    tiledLayer.levelsOfDetailBias = 1000;
    tiledLayer.frame = pageRect;

    contentView = [[UIView alloc] initWithFrame:pageRect];
    [contentView.layer addSublayer:tiledLayer];

    CGRect viewFrame = self.view.frame;
    viewFrame.origin = CGPointZero;

    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];

    //[scrollView setBackgroundColor:[UIColor cyanColor]];
    //[contentView setBackgroundColor:[UIColor yellowColor]];

    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.maximumZoomScale = 1000;
    [scrollView addSubview:contentView];

    [self.view addSubview:scrollView];   

    pagingViewController = [[PDFPagingViewController alloc] initWithDocument:self._document AndObserver:self];
    [self.view addSubview:pagingViewController.view];
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return contentView;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    if(self._document) {
        [layer setBackgroundColor:(CGColorRef)[UIColor redColor]];

        CGRect drawingRect = CGContextGetClipBoundingBox(ctx);

        CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
        CGContextFillRect(ctx, drawingRect);
        CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(self._document.page, kCGPDFCropBox, layer.bounds, 0, true));
        CGContextDrawPDFPage(ctx, self._document.page);
    }
}

Upvotes: 0

Views: 1013

Answers (1)

thelaws
thelaws

Reputation: 8001

You need to change when you set the pageRect's origin (which I assume you do to center in the view). You are setting both the layer's frame and the contentView's frame to the offset pageRect when you really intend to offset only one.

-(void)refreshPage {
//...

CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(self._document.page, kCGPDFCropBox));

CATiledLayer *tiledLayer = [CATiledLayer layer];
//...
tiledLayer.frame = pageRect;

// set the pageRect origin now to center the contentView, not the layer
pageRect.origin.x = ((self.view.frame.size.width / 2) - (pageRect.size.width / 2));

contentView = [[UIView alloc] initWithFrame:pageRect];
[contentView.layer addSublayer:tiledLayer];

//...
}

Upvotes: 2

Related Questions