PDF 和缩放 - CATiledLayer

发布于 2024-11-17 22:04:25 字数 2636 浏览 3 评论 0原文

我有一个问题,我不是程序员,但我尝试做一些应用程序来娱乐。 我尝试创建一个pdf查看器,我创建一个像这样的UIView类:

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:(CGRect)frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
        NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
        document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
        currentPage = 1;
    }
    return self;
}

-(void)drawRect:(CGRect)inRect{                 
    if(document) {
        CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        CGContextTranslateCTM(ctx, 0.0, [self bounds].size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, [self bounds], 0, true));
        CGContextDrawPDFPage(ctx, page);    
        CGContextRestoreGState(ctx);
    }
}

-(void)increasePageNumber {
    size_t pageCount = CGPDFDocumentGetNumberOfPages(document);
    if (currentPage == pageCount) {
        // do nothing
    }
    else {
        currentPage++;
        [self setNeedsDisplay];
    }
}

-(void)decreasePageNumber {
    if (currentPage == 1) {
        // do nothing
    }
    else {
        currentPage--;
        [self setNeedsDisplay];
    }
}

现在我尝试添加缩放但没有运气,从缩放我创建一个图层:

-(void)viewDidLoad {
    CGRect frame;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        frame = CGRectMake(0, 0, 960, 1024);

    }
    else{
        frame = CGRectMake(0, 0, 320, 460);

    }

    tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024, 1024);
    tiledLayer.levelsOfDetail = 200;
    tiledLayer.levelsOfDetailBias = 200;
    tiledLayer.frame = frame;

    myPDFView = [[viewPDF alloc] initWithFrame:frame];   
    //[self.view addSubview:myPDFView];  

    [myPDFView.layer addSublayer:tiledLayer];

    CGRect viewFrame = self.view.frame;
    viewFrame.origin = CGPointZero;
    scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
    scrollView.delegate = self;
    scrollView.contentSize = frame.size;
    scrollView.maximumZoomScale = 5;
    [scrollView addSubview:myPDFView];

    [self.view addSubview:scrollView];

}

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

编辑:缩放工作,我使用shouldAutorotateToInterfaceOrientation这就是问题(我需要重写一些代码),现在它可以工作,但是当我缩放质量变差时。 :(

I have a problem, I'm not a programmer but I try to do some app for fun.
I try to create a pdf viewer, I create a UIView class like this:

- (id)initWithFrame:(CGRect)frame {

    self = [super initWithFrame:(CGRect)frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
        NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
        document = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
        currentPage = 1;
    }
    return self;
}

-(void)drawRect:(CGRect)inRect{                 
    if(document) {
        CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage);
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        CGContextSaveGState(ctx);
        CGContextTranslateCTM(ctx, 0.0, [self bounds].size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, [self bounds], 0, true));
        CGContextDrawPDFPage(ctx, page);    
        CGContextRestoreGState(ctx);
    }
}

-(void)increasePageNumber {
    size_t pageCount = CGPDFDocumentGetNumberOfPages(document);
    if (currentPage == pageCount) {
        // do nothing
    }
    else {
        currentPage++;
        [self setNeedsDisplay];
    }
}

-(void)decreasePageNumber {
    if (currentPage == 1) {
        // do nothing
    }
    else {
        currentPage--;
        [self setNeedsDisplay];
    }
}

now I try to add zoom but without luck, fro zoom I create a layer:

-(void)viewDidLoad {
    CGRect frame;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        frame = CGRectMake(0, 0, 960, 1024);

    }
    else{
        frame = CGRectMake(0, 0, 320, 460);

    }

    tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024, 1024);
    tiledLayer.levelsOfDetail = 200;
    tiledLayer.levelsOfDetailBias = 200;
    tiledLayer.frame = frame;

    myPDFView = [[viewPDF alloc] initWithFrame:frame];   
    //[self.view addSubview:myPDFView];  

    [myPDFView.layer addSublayer:tiledLayer];

    CGRect viewFrame = self.view.frame;
    viewFrame.origin = CGPointZero;
    scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
    scrollView.delegate = self;
    scrollView.contentSize = frame.size;
    scrollView.maximumZoomScale = 5;
    [scrollView addSubview:myPDFView];

    [self.view addSubview:scrollView];

}

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

EDIT: the zoom work, I use shouldAutorotateToInterfaceOrientation that was the problem (I need to rewrite some code), now it work BUT when I zoom quality worsens. :(

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文