0

私はPDFリーダーを作るつもりで、2つの可能な解決策を試しています:

  • UIWebView
  • CGPDFDocumentRef

最初のものは非常に単純ですが、2 番目のものよりも遅くなります。2 つ目については、チュートリアルを見つけて、pdf を表示でき、2 本の指の動きでズームできます。しかし、現時点では私は2つのことを行うことができません:

1) 正しい縦横比を維持しながら、横向きモードで PDF を表示します。2) 画面のダブルタップでズームイン/ズームアウトします。

これらの関数は既に UIWebView に実装されていますが、前に述べたように、少し遅く、単一のページだけでなく PDF ドキュメント全体を開きます (そのため、ファイルを多くの「1 ページの pdf ファイル」に分割する必要があります)。

この種の問題に関するリンク/参照/コードを手伝ってくれる人はいますか?

4

3 に答える 3

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に描画するなどの方法があります。

于 2010-07-24T16:18:44.060 に答える
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));
}

私は事実上すべてを試しましたが、良い解決策が見つかりません!

于 2010-07-24T20:28:21.153 に答える
0

[yourLayer setNeedsDisplay] と言って、任意のレイヤーの描画レイヤーメソッドを手動で呼び出すことができます

于 2011-01-18T13:46:18.127 に答える