使用drawRect和drawLayerInContext在iOS中渲染PDF [英] Rendering PDF in iOS using drawRect and drawLayerInContext

查看:240
本文介绍了使用drawRect和drawLayerInContext在iOS中渲染PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个自定义视图,该视图将PDF渲染到其bounds矩形.
我粘贴了以下代码:

I'm creating a custom view that renders a PDF to its bounds rectangle .
I have pasted the code below :

CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context, self.bounds);

CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CGFloat scale = MIN(self.bounds.size.width / pageRect.size.width, self.bounds.size.height / pageRect.size.height);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextScaleCTM(context, scale, scale);    
CGContextDrawPDFPage(context, self.page);
CGContextRestoreGState(context);

我的问题是,如果我在drawRect方法中使用上面的代码,它只会给我一个黑色矩形.如果我在drawLayer:inContext:方法中使用它,则效果很好.可能是什么原因 ?

PS:使用drawRect时,我使用UIGraphicsGetCurrentContext()方法获取当前图形上下文.

My question is , if I use the above code in the drawRect method it just gives me a black rectangle . If I use it in the drawLayer: inContext: method it works fine . What could be the reason ?

PS : When using it drawRect I use the method UIGraphicsGetCurrentContext() to get the current graphics context .

推荐答案

此代码对我来说实际上很好.. 添加断点并检查其是否符合-drawrect方法

This code actually works fine for me .. Add breakpoint and check it hits the -drawrect method

 CGContextRef context = UIGraphicsGetCurrentContext();
NSString *filePath = [[NSBundle mainBundle]
                      pathForResource:@"Trial" ofType:@"pdf"];
NSURL *url = [[NSURL alloc] initFileURLWithPath:filePath];
CGPDFDocumentRef document = [self openDocument:(CFURLRef)url];
[url release];

CGPDFPageRef docPageRef =CGPDFDocumentGetPage(document, 1);

CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context, self.bounds);

CGRect pageRect = CGPDFPageGetBoxRect(docPageRef, kCGPDFMediaBox);
CGFloat scale = MIN(self.bounds.size.width / pageRect.size.width, self.bounds.size.height / pageRect.size.height);
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextScaleCTM(context, scale, scale);
CGContextDrawPDFPage(context, docPageRef);
CGContextRestoreGState(context);

我在我的drawrect中使用了它,并且效果很好.

I used this in my drawrect and it worked fine.

检查所有引用是否都获得了您提供给drawrect的值 选中此

Check all the references are getting the value that you are providing to the drawrect check this

这篇关于使用drawRect和drawLayerInContext在iOS中渲染PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆