CGPDFViewController 和多页 PDF(缩放)

发布于 2024-11-04 19:20:42 字数 3133 浏览 0 评论 0原文

我正在尝试为 iOS 创建一个基本的 PDF 查看器,但我几乎陷入困境。我只希望它能够查看 PDF 并跳转到特定页面。

这是我到目前为止所做的:

#import "SFPDFViewerController.h"
#import <QuartzCore/QuartzCore.h>

@implementation SFPDFViewerController
@synthesize currentPageIndex;

- (void)viewDidLoad
{
    currentPageIndex = 1;

    myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL URLWithString:@"file:///Users/soulstorm/Desktop/Basic%20UNIX%20Tutorial.pdf"]);
    myPageRef = CGPDFDocumentGetPage(myDocumentRef, currentPageIndex);
    //CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox));
    CGRect pageRect = self.view.bounds;

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

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

    CGRect viewFrame = self.view.frame;
    viewFrame.origin = CGPointZero;
    viewFrame.size.height-= 44;

    scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.maximumZoomScale = 3.0;
    [scrollView addSubview:myContentView];

    [self.view addSubview:scrollView];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    [scrollView setBackgroundColor:[UIColor whiteColor]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    NSLog(@"draw layer...");
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
    CGContextDrawPDFPage(ctx, myPageRef);
}

#pragma mark - Functions
- (IBAction)goToNextPage:(id)sender
{
    [self displayNextPage];
}
- (IBAction)goToPreviewsPage:(id)sender
{
    [self displayPreviewsPage];
}

- (void)displayNextPage
{
    currentPageIndex++;
    [self reloadCurrentPage];
}
- (void)displayPreviewsPage
{
    currentPageIndex--;
    [self reloadCurrentPage];
}
- (void)reloadCurrentPage
{
    NSLog(@"reloading current page... %i", currentPageIndex);
    myPageRef = CGPDFDocumentGetPage(myDocumentRef, currentPageIndex);
    //[self.view setNeedsDisplay];
    //[self.view.layer setNeedsDisplay];
    //[myContentView.layer setNeedsDisplay];
}
#pragma mark -
- (void)dealloc
{
    [myContentView release];
    myContentView = nil;
    CGPDFDocumentRelease(myDocumentRef);
    myDocumentRef = NULL;
    myPageRef = NULL;
    [super dealloc];
}
@end

上面的代码正确显示了 PDF 的第一页,但是当我尝试转到下一页时,它将保留在第一页上,有时在缩放时我可以看到 PDF 的部分内容第二页。

有人能指出我正确的方向吗?

I am trying to create a basic PDF viewer for iOS, and I am pretty much stuck. I only want it to be able to view PDFs and jump to a specific page.

Here is what I have done so far:

#import "SFPDFViewerController.h"
#import <QuartzCore/QuartzCore.h>

@implementation SFPDFViewerController
@synthesize currentPageIndex;

- (void)viewDidLoad
{
    currentPageIndex = 1;

    myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL URLWithString:@"file:///Users/soulstorm/Desktop/Basic%20UNIX%20Tutorial.pdf"]);
    myPageRef = CGPDFDocumentGetPage(myDocumentRef, currentPageIndex);
    //CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox));
    CGRect pageRect = self.view.bounds;

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

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

    CGRect viewFrame = self.view.frame;
    viewFrame.origin = CGPointZero;
    viewFrame.size.height-= 44;

    scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.maximumZoomScale = 3.0;
    [scrollView addSubview:myContentView];

    [self.view addSubview:scrollView];

    [self.view setBackgroundColor:[UIColor whiteColor]];
    [scrollView setBackgroundColor:[UIColor whiteColor]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

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

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    NSLog(@"draw layer...");
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
    CGContextDrawPDFPage(ctx, myPageRef);
}

#pragma mark - Functions
- (IBAction)goToNextPage:(id)sender
{
    [self displayNextPage];
}
- (IBAction)goToPreviewsPage:(id)sender
{
    [self displayPreviewsPage];
}

- (void)displayNextPage
{
    currentPageIndex++;
    [self reloadCurrentPage];
}
- (void)displayPreviewsPage
{
    currentPageIndex--;
    [self reloadCurrentPage];
}
- (void)reloadCurrentPage
{
    NSLog(@"reloading current page... %i", currentPageIndex);
    myPageRef = CGPDFDocumentGetPage(myDocumentRef, currentPageIndex);
    //[self.view setNeedsDisplay];
    //[self.view.layer setNeedsDisplay];
    //[myContentView.layer setNeedsDisplay];
}
#pragma mark -
- (void)dealloc
{
    [myContentView release];
    myContentView = nil;
    CGPDFDocumentRelease(myDocumentRef);
    myDocumentRef = NULL;
    myPageRef = NULL;
    [super dealloc];
}
@end

The above code displays the first page of the PDF corectly, but when I try to go to the next page, it will remain on the first one, and sometimes when zooming I can see parts of the second page.

Can anyone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文