在缩放ios上将内容添加到pdf的正确方法

时间:2016-09-14 09:57:48

标签: objective-c pdf catiledlayer cgpdfdocumentref

我正在一个以pdf显示地图的应用程序中工作。因为我有基本地图pdf (pdf1),我需要在其上显示另一个具有城市名称的pdf (pdf2)图层。

我可以通过在视图上添加另一个CATiledLayer对象来实现,如下所示:

 NSString *filePath = [[NSBundle mainBundle]  pathForResource:fileName ofType:@"pdf"];
NSURL *pdfURL = [NSURL fileURLWithPath:filePath];
CGPDFDocumentRef myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);
_PDFPageRef =  CGPDFDocumentGetPage(myDocumentRef, 1);


CATiledLayer *tiledLayer = [CATiledLayer layer];
tiledLayer.delegate = self;
tiledLayer.levelsOfDetail = 3; // Zoom levels

tiledLayer.levelsOfDetailBias = 3; // Bias
tiledLayer.backgroundColor = [UIColor clearColor].CGColor;
UIScreen *mainScreen = [UIScreen mainScreen]; // Main screen

CGFloat screenScale = [mainScreen scale]; // Main screen scale

CGRect screenBounds = [mainScreen bounds]; // Main screen bounds

CGFloat w_pixels = (screenBounds.size.width * screenScale);

CGFloat h_pixels = (screenBounds.size.height * screenScale);

CGFloat max = ((w_pixels < h_pixels) ? h_pixels : w_pixels);

CGFloat sizeOfTiles = ((max < 512.0f) ? 512.0f : 1024.0f);

tiledLayer.tileSize = CGSizeMake(sizeOfTiles, sizeOfTiles);
tiledLayer.frame =  CGRectIntegral(CGPDFPageGetBoxRect(_PDFPageRef,  kCGPDFCropBox));
[[self layer] addSublayer:tiledLayer];
[self setNeedsDisplay];

但我在这里遇到两个问题:

  1. 如果pdf1没有完全加载,我在其上面添加了另一个pdf2,那么pdf1将永远不会被完全加载。它会模糊不清。

  2. 如果完全加载了pdf1,那么在添加pdf2并在缩放3,4次后进行缩放时,pdf1会变得模糊。

  3. Map with pdf1 Map with pdf2 added on pdf1

    请帮助!!

1 个答案:

答案 0 :(得分:0)

我已经获得了在缩放时为pdf添加更多内容的解决方案。根据我的理解,我错误的是我在同一个视图上添加了另一个CATiledLayer (tilesLayer)对象,并且该视图正在考虑此对象作为当前图块和视图正在处理此图块<强>(TiledLayer的)即可。这就是为什么前一个瓷砖的褪色视图不清晰。

现在我使用相同的磁贴在其上添加更多详细信息。我使用了另一个CGPDFPageRef对象(_ PDFPageRef2)

CGPDFDocumentRef myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);

_PDFPageRef2 =  CGPDFDocumentGetPage(myDocumentRef, 1);

在此之后,我在视图

上调用 setNeedsDisplay 方法
[self setNeedsDisplay];

此方法再次绘制视图,因此调用视图的 -drawRect:方法,其实现如下:

- (无效)的drawRect:(的CGRect)RECT {     CGContextRef context = UIGraphicsGetCurrentContext();

float dpi = 100.0 / 72.0;

ReaderContentPage *readerContentPage = self; // Retain self

//  CGContextSetRGBFillColor(context, 1.0f, 1.0f, 1.0f, 1.0f); // White
CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
CGContextFillRect(context, CGContextGetClipBoundingBox(context)); // Fill

//NSLog(@"%s %@", __FUNCTION__, NSStringFromCGRect(CGContextGetClipBoundingBox(context)));

CGContextTranslateCTM(context, 0.0f, self.bounds.size.height); CGContextScaleCTM(context, 1.0f, -1.0f);


CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(_PDFPageRef, kCGPDFCropBox, self.bounds, 0, true));

//CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); CGContextSetInterpolationQuality(context, kCGInterpolationDefault);

CGContextDrawPDFPage(context, _PDFPageRef); // Render the PDF1 page into the context
if (_PDFPageRef2) {
    CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(_PDFPageRef2, kCGPDFCropBox, self.bounds, 0, true));

    //CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); CGContextSetInterpolationQuality(context, kCGInterpolationDefault);

    CGContextDrawPDFPage(context, _PDFPageRef2); // Render the PDF2 page into the context

}
if (readerContentPage != nil) readerContentPage = nil; // Release self
                                                       //    UIGraphicsPushContext(context);
                                                       //    CGSize size = CGSizeMake(320, 480); //Screen Size on iPhone device
                                                       //    UIGraphicsBeginImageContext(size);  //Create a new Bitmap-based graphics context (also makes this the current context)

}

因此,我可以在缩放时为pdf添加更多细节。

enter image description here