0

我正在一个以 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 变得模糊。

带有pdf1的地图 在 pdf1 上添加 pdf2 的地图

请帮忙!!

4

1 回答 1

0

我已经得到了在缩放时向 pdf 添加更多内容的解决方案。根据我的理解,我做错的是我在同一个视图上添加了另一个 CATiledLayer (tiledLayer)对象,并且视图正在考虑这个对象作为当前的图块和视图正在处理这个图块(tiledLayer)。这就是为什么前一个瓷砖的褪色视图不清晰的原因。

现在我使用相同的磁贴在其上添加更多细节。我使用了另一个 CGPDFPageRef 对象(_PDFPageRef2)

CGPDFDocumentRef myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef) pdfURL);

_PDFPageRef2 =  CGPDFDocumentGetPage(myDocumentRef, 1);

在此之后,我在视图上调用setNeedsDisplay方法

[self setNeedsDisplay];

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

-(void)drawRect:(CGRect)rect { CGContextRef 上下文 = 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 中添加更多关于缩放的细节。

在此处输入图像描述

于 2016-09-15T06:24:37.020 回答