2010-07-24 45 views
0
PDF閱讀器

我要去做出一個PDF閱讀器,我想兩個可能的解決方案:
與CGPDFDocumentRef

  • UIWebView的
  • CGPDFDocumentRef

第一個是非常簡單的,但比第二個慢。 對於第二個,我找到了一個教程,我可以查看一個pdf,我可以用兩個手指移動它。但目前我不能做2件事:

1)在橫向模式下查看PDF,保持正確的比例。 2)使用屏幕上的雙擊進行放大/縮小。

這些函數已經在UIWebView中實現了,但是之前說過,它有點慢,它打開整個PDF文檔,而不僅僅是一個頁面(所以我需要將文件分成許多「一頁pdf文件「)。

任何人都可以幫助我解決關於這些問題的一些鏈接/引用/代碼?

+0

u能請提供土特鏈接繪製任意層的層的方法? – 2012-12-10 07:54:29

+0

請提供教程鏈接 – 2013-05-10 07:18:12

回答

3

使用CGPDFDocumentRef,您需要獲取每個頁面CGPDFDocumentGetPage,然後在繪製頁面之前將其應用於CGPDFPageGetDrawingTransform並將其應用於CGContextRef

如果啓用縮放,將視圖與PDF頁面放在UIScrollView中可讓您放大和縮小。您也可以使用

CGContextTranslateCTM (context, -sourceRect.origin.x * zoom, -sourceRect.origin.y * zoom); 
    CGContextScaleCTM (context, zoom, zoom); 

變焦倍率畫,但你還是會碰到的是非常複雜的PDF頁面將是緩慢的渲染問題,每次通話時間CGContextDrawPDFPage(context, page);它使整個頁面例如當您更改縮放級別時。有關於此的方法,例如繪製到CATiledLayer中。

+0

您確定整個頁面將在每次抽獎時呈現嗎?渲染器應該足夠聰明,以便在渲染修剪區域時不處理所有的pdf數據,不是? – Felixyz 2011-10-11 19:46:22

1

確切地說,在我找到的例子中,他使用了CATiledLayer。
這裏有我的代碼:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    CFURLRef pdfURL = CFBundleCopyResourceURL(
               CFBundleGetMainBundle(), 
               CFSTR(filename), 
               CFSTR("pdf"), NULL); 
    myDocumentRef = CGPDFDocumentCreateWithURL(pdfURL); 
    myPageRef = CGPDFDocumentGetPage(myDocumentRef, 1); 
    CGRect pageRect = CGRectMake(0, 0, 320, 480); 

    tiledLayer = [CATiledLayer layer]; 
    tiledLayer.delegate = self; 
    tiledLayer.tileSize = CGSizeMake(1024.0, 1024.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; 
    scrollView = [[UIScrollView alloc] initWithFrame:viewFrame]; 
    scrollView.delegate = self; 
    scrollView.contentSize = pageRect.size; 
    scrollView.maximumZoomScale = 100; 
    [scrollView addSubview:myContentView]; 

    [self.view addSubview:scrollView]; 
} 

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

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 
    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); 
} 

這部分的偉大工程,但如果我要爲大家介紹的景觀模式,我需要修改這一部分:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    if((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
     (interfaceOrientation == UIInterfaceOrientationLandscapeRight)){ 
     //SOMETHING TO DO FOR LANDSCAPE MODE 
    } 
    else if((interfaceOrientation == UIInterfaceOrientationPortrait)){ 
     //SOMETHING TO DO TO RESTORE TO PORTRAIT 
    } 
    // Return YES for supported orientations 
    return ((interfaceOrientation == UIInterfaceOrientationPortrait) || 
      (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) || 
      (interfaceOrientation == UIInterfaceOrientationLandscapeRight)); 
} 

我試過pratically一切,但我找不到一個好的解決方案!

+0

如何保存方向,然後在繪製PDF時,如果是橫向,則使用CGContextRotateCTM旋轉PDF。 – jtbandes 2010-07-24 20:30:55

+0

好吧,但問題是,如果我在shouldAutorotateToInterfaceOrientation方法內調用CGContextRotateCTM,轉換不會!我發現應用轉換的唯一方法是將其放入drawLayer方法中。 (我不知道我可以手動記得它)任何建議或詭計? – Marco 2010-07-25 08:58:33

+0

是否有任何方式顯示所有的頁面,就像uiwebview一樣。 – Sat 2015-01-23 09:39:31

0

你可以叫手動說 [yourLayer setNeedsDisplay]

相關問題