我试图显示水平放置的单页PDF,以便可以通过向左或向右滑动来浏览页面。当然,我需要缩放页面。
我在启用分页的容器UIWebView
中使用UIScrollViewer
来实现它。 UIWebView
用作PDF查看器,并且UIScrollView
处理页面更改。但是这种实现不是一个好方法,它使用了过多的内存,因此我决定使用Apple的ZoomingPDFViewer
而不是UIWebView
。
我的带有UIWebView
的原始代码如下所示:
for (int i=0; i<n; i++)
{
CGRect frame;
frame.origin.y = 0;
frame.origin.x = self.scrollviewer.frame.size.width * i;
frame.size = self.scrollviewer.frame.size;
UIWebView *pageViewer = [[UIWebView alloc] initWithFrame:frame];
pageViewer.scalesPageToFit = TRUE;
pageViewer.multipleTouchEnabled = TRUE;
[self.scrollviewer addSubview:pageViewer];
NSURL *url = [NSURL URLWithString:address];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
[pageViewer loadRequest:req];
}
self.scrollviewer.contentSize = CGSizeMake(nPages * self.scrollviewer.frame.size.width, self.scrollviewer.frame.size.height);
这很完美,但是当我有25页以上时,它很慢,并且Apple不建议在
UIWebView
或UIScrollView
中使用UITableView
,因为触摸事件可能会产生意想不到的结果。因此,我想将
UIWebView
更改为Apple示例中给出的ZoomingPDFViewer
。我的新代码ZoomingPDFViewer
看起来像这样:for (int i=0; i<n; i++)
{
CGRect frame;
frame.origin.y = 0;
frame.origin.x = self.scrollviewer.frame.size.width * i;
frame.size = self.scrollviewer.frame.size;
PDFScrollView *pageViewer = [[PDFScrollView alloc] initWithFrame:frame];
[self.scrollviewer addSubview:pageViewer];
NSURL *url = [NSURL URLWithString:address];
CGPDFDocumentRef PDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);
CGPDFPageRef PDFPage = CGPDFDocumentGetPage(PDFDocument, 1);
[(PDFScrollView *)pageViewer setPDFPage:PDFPage];
CGPDFDocumentRelease(PDFDocument);
}
self.scrollviewer.contentSize = CGSizeMake(nPages * self.scrollviewer.frame.size.width, self.scrollviewer.frame.size.height);
此代码显示了我的PDF页面,可以在页面之间滑动,但是无法放大页面。
我不知道该怎么办。你能帮助我吗?
最佳答案
我找到了解决方案。
问题是,当您以编程方式创建UIScrollView
时,默认情况下会设置minimumZoomScale
和maximumZoomScale
1.0。所以实际上我的代码在工作,但是我不知道它在工作,因为它没有放大或缩小。
所以我在我的代码中添加了两行,就可以了!这是我的代码中的更改
PDFScrollView *pageViewer = [[PDFScrollView alloc] initWithFrame:frame];
pageViewer.minimumZoomScale = 0.2;
pageViewer.maximumZoomScale = 5.0;
[self.scrollviewer addSubview:pageViewer];
当我插入两行时,它可以工作,但是我可以无限放大或缩小。我想以最大比例5缩放。因此,我更改了文件
scrollViewDidEndZooming
中给出的PDFScrollView.m
方法Apple的
ZoomingPDFViewer
随附的默认代码如下所示:// Set the new scale factor for the TiledPDFView.
_PDFScale *= scale;
// Calculate the new frame for the new TiledPDFView.
CGRect pageRect = CGPDFPageGetBoxRect(_PDFPage, kCGPDFMediaBox);
pageRect.size = CGSizeMake(pageRect.size.width*_PDFScale, pageRect.size.height*_PDFScale);
更改后,它看起来像:
// Set the new scale factor for the TiledPDFView.
_PDFScale *= scale;
self.minimumZoomScale *= 1 / scale;
self.maximumZoomScale *= 1 / scale;
// Calculate the new frame for the new TiledPDFView.
CGRect pageRect = CGPDFPageGetBoxRect(_PDFPage, kCGPDFMediaBox);
pageRect.size = CGSizeMake(pageRect.size.width*_PDFScale, pageRect.size.height*_PDFScale);
现在可以了。
关于iphone - ZoomingPDFViewer无法在启用分页的UIScrollViewer中缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15627107/