在iOS中使用drawRect和drawLayerInContext呈现PDF

我正在创build一个自定义视图,将PDF渲染到其边界矩形。
我已经粘贴下面的代码:

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()来获取当前的graphics上下文。

这段代码实际上对我来说工作正常。添加断点并检查它是否符合-drawrect方法

  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中使用它,它工作正常。

检查所有参考正在获取您提供给拉丁的值检查这一点