iPhone sdk的PDF页面绘图问题

iPhone sdk的PDF页面绘图问题,iphone,Iphone,在iPhone中实现PDF文件时,我遇到了一个问题 我想要的只是显示PDF文件。而且还提供了一些功能,如放大和缩小、下一页导航、上一页导航等 我非常清楚使用url阅读PDF文档,我还可以获得页面数和其他属性,但当我尝试在View或WebView中显示它时,我的意思是说,当我尝试绘制PDF页面时,我没有获得页面,只是获得一个空视图 为了显示页面,我尝试了5种不同的方法,但都没有成功。所以我必须接近你们 我在这里附上了我使用的5种不同方法的代码片段 请通过它引导我!!欢迎你的建议 /////////

在iPhone中实现PDF文件时,我遇到了一个问题

我想要的只是显示PDF文件。而且还提供了一些功能,如放大和缩小、下一页导航、上一页导航等

我非常清楚使用url阅读PDF文档,我还可以获得页面数和其他属性,但当我尝试在View或WebView中显示它时,我的意思是说,当我尝试绘制PDF页面时,我没有获得页面,只是获得一个空视图

为了显示页面,我尝试了5种不同的方法,但都没有成功。所以我必须接近你们

我在这里附上了我使用的5种不同方法的代码片段

请通过它引导我!!欢迎你的建议

////////////////////////////

- (void)viewDidLoad {
    [super viewDidLoad];

 const char *file = @"Pdf_first.pdf";

 CGPDFDocumentRef myDocument;
 CGPDFPageRef page;
 CGRect mediaBox;
  CFStringRef path;
 size_t noOfPages;
    CFURLRef url;
 CGContextRef pdfContext;



//////// Code to get Path and Url for the dictionary where our PDF file currently stored. /////////

 NSFileManager *FileManager = [NSFileManager defaultManager];
 NSArray *paths1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths1 objectAtIndex:0];
 documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Pdf_first.pdf"];

 path = documentsDirectory;

    url = CFURLCreateWithFileSystemPath (NULL, path,kCFURLPOSIXPathStyle, 0);
 myDocument = CGPDFDocumentCreateWithURL(url);
 myDocument = CGPDFDocumentRetain(myDocument);


 CFMutableDictionaryRef myDictionary = NULL;
 myDictionary = CFDictionaryCreateMutable(NULL, 0,&kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks);
 CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
 CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name"));



 page = CGPDFDocumentGetPage(myDocument, 1);
 noOfPages = CGPDFDocumentGetNumberOfPages(myDocument);
 pdfContext = CGPDFContextCreateWithURL(url, NULL, myDictionary);
 CGRect pageMediaBox = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 

//////// Code to get Path and Url for the dictionary where our PDF file currently stored. /////////
这里一切都很好!!我从函数中获取所有值。下面我添加了5种不同的方法 我跟随它来显示或绘制我可以在iphone中看到的页面

////////////////////////// Way 1 ///////////////////////

 CGContextTranslateCTM(pdfContext, 0.0, [webView bounds].size.height);
 CGContextScaleCTM(pdfContext, 1.0, -1.0);
 CGContextConcatCTM(pdfContext, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox,[webView bounds], 0, true));
 CGContextDrawPDFPage(pdfContext, page);    
 CGContextRestoreGState(pdfContext);

 ////////////////////////// Way 1 ///////////////////////


////////////////////////// Way 2 ///////////////////////

 CGRect bounds = CGRectMake(10, 10, 300, 300);

 CGContextSaveGState(pdfContext);
 CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, bounds, 0, true);
 CGContextConcatCTM(pdfContext, pdfTransform);
 CGContextDrawPDFPage(pdfContext, page);
 CGContextRestoreGState(pdfContext);

 ////////////////////////// Way 2 ///////////////////////

///////////////////////////////////// Way 3 ////////////////////


    for(int i = 1; i <= noOfPages; ++i) {

        CGPDFPageRef pdfPage =  CGPDFDocumentGetPage(myDocument, i);

        CGRect pageMediaBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); 

        CGContextBeginPage (pdfContext, &pageMediaBox); 

  CGContextStrokeRect(pdfContext, CGRectMake(50, 50, 500,700)); 
  CGContextShowTextAtPoint (pdfContext, 60, 699, text, strlen(text));

  CGContextDrawPDFPage(pdfContext, pdfPage);

        CGContextEndPage (pdfContext); 

    }

  ///////////////////////////////////// Way 3 ////////////////////

 /////////////////////////////// Way 4 ////////////////////////////////

 //mediaBox = CGPDFDocumentGetMediaBox(document, 1);

 CGPDFBox mediaBox = CGPDFDocumentGetMediaBox(document, 1);

 //mediaBox = CGRectMake(10,10,300,300);
 // int rotationAngle = CGPDFDocumentGetRotationAngle(document, 1);

 int rotationAngle = 30;

 //CGContextDrawPDFDocument(UIGraphicsGetCurrentContext(), CGRectMake(25,25,250,250), document, 1);

 //CGPDFPageGetDrawingTransform(<#CGPDFPageRef page#>, <#CGPDFBox box#>, <#CGRect rect#>, <#int rotate#>, <#_Bool preserveAspectRatio#>)

 CGAffineTransform transform = CGPDFPageGetDrawingTransform(page, mediaBox, CGRectMake(25, 25, 250,250), rotationAngle, TRUE);


   /////////////////////////////// Way 4 ////////////////////////////////

///////////////////////// Way 5 /////////////////////////////

 CGContextTranslateCTM(pdfContext, 0.0, 320);

 CGContextScaleCTM(pdfContext, 1.0, -1.0);

 CGContextSaveGState(pdfContext);

 CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, 280, 320), 0, true);

 CGContextConcatCTM(pdfContext, pdfTransform);

 CGContextDrawPDFPage(pdfContext, page);

 CGContextRestoreGState(pdfContext);

    ///////////////////////// Way 5 /////////////////////////////
//路1///////////////////////
CGContextTranslateCm(pdfContext,0.0,[webView界限].size.height);
CGContextScaleCTM(pdfContext,1.0,-1.0);
CGContextConcatCTM(pdfContext,cgpfpagegetDrawingTransform(第页,kCGPDFCropBox,[webView-bounds],0,true));
CGContextDrawPDFPage(pdfContext,第页);
CGContextRestoreGState(pdfContext);
//////////////////////////方式1///////////////////////
//////////////////////////方式2///////////////////////
CGRect边界=CGRectMake(10,1030300);
CGContextSaveGState(pdfContext);
CGAffineTransform PDFTTransform=CGPDFPageGetDrawingTransform(第页,kCGPDFCropBox,边界,0,true);
CGContextConcatCTM(pdfContext,PDFTTransform);
CGContextDrawPDFPage(pdfContext,第页);
CGContextRestoreGState(pdfContext);
//////////////////////////方式2///////////////////////
/////////////////////////////////////方式3////////////////////

对于(int i=1;i您不能在-viewDidLoad中绘制视图。此时没有CGContext。您所有的绘图代码都需要进入-drawRect。您想阅读。

您不能在-viewDidLoad中绘制视图。此时没有CGContext。您所有的绘图代码都需要进入-drawRect。您想阅读。

什么是kCGPDFCropBox那不是为我编译的。什么是不是为我编译的kCGPDFCropBox。