2012-03-31 112 views
-1

我使用下面的代碼來繪製和旋轉PDF文件。它與iOS 5.x完美無瑕,但在iOS 4.3下,只有一個白頁出現在調試器中的錯誤:iOS 4.3 PDF文件呈現問題

無效`Contents':不是一個數組流。

錯誤發生在「CGContextDrawPDFPage(context,pdfPage)」之後;

爲什麼它可以與iOS 5.x一起使用,但不是4.3.x?我嘗試了不同的PDF文件,但仍得到相同的結果。

我該如何解決這個問題?

pdfpage定義爲:

- (void)setPage:(CGPDFPageRef)newPage 
{ 
    CGPDFPageRelease(self->pdfPage); 
    self->pdfPage = CGPDFPageRetain(newPage); 
} 

在那裏發生的方法,包括:

-(void)drawLayer:(CALayer*)layer inContext:(CGContextRef)context 
{ 
    // First fill the background with white. 
    CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
    CGContextFillRect(context,self.bounds); 
    CGContextSaveGState(context); 
    int rotate = CGPDFPageGetRotationAngle(pdfPage); 

    switch (rotate) { 
     case 0: 
      CGContextTranslateCTM(context, 0.0, self.bounds.size.height); 
      CGContextScaleCTM(context, 1, -1); 
      break; 
     case 90: 
      CGContextScaleCTM(context, 1.0, -1.0); 
      CGContextRotateCTM(context, -M_PI/2); 
      break; 
     case 180: 
     case -180: 
      CGContextScaleCTM(context, 1, -1); 
      CGContextTranslateCTM(context, self.bounds.size.width, 0); 
      CGContextRotateCTM(context, M_PI); 
      break; 
     case 270: 
     case -90: 
      CGContextTranslateCTM(context, self.bounds.size.height, self.bounds.size.width); 
      CGContextRotateCTM(context, M_PI/2); 
      CGContextScaleCTM(context, -1, 1); 
      break; 
    } 

    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 
    CGContextSetRenderingIntent(context, kCGRenderingIntentDefault); 
    CGContextScaleCTM(context, myScale,myScale); 
    CGContextDrawPDFPage(context, pdfPage); // the error shows up right after executing this line 
    CGContextRestoreGState(context); 
} 
+1

什麼是'pdfPage'?檢查它是否正確初始化。 – 2012-03-31 07:35:03

+0

請注意,向下/向上投票不幫助我解決問題! – 2012-03-31 07:36:21

+0

@MitulNakum其初始化正確它與ios 5.x但不是ios 4.3.x我有更新代碼。感謝您試圖提供幫助。 – 2012-03-31 07:39:04

回答

0

據PDF說明書中,頁字典的/目錄條目可以是流對象或流對象數組。根據錯誤消息,似乎iOS 4.3沒有正確實現PDF規範,它總是期望/ Contents項的一組數據流,並且您的文件使用單個流對象。 iOS 5可能解決了這個問題。

+0

我回到了蘋果pdf渲染的例子,現在它也適用於ios 4.3。但我沒有得到它是什麼問題。無論如何,如果有人遇到這個問題,就拿着蘋果的例子 – 2012-04-03 19:45:06

+1

花幾個小時試圖找出兩個樣本(ZoomingPDFViewer)之間有什麼區別,我終於找到了它。在iOS 5之前,您需要保留對PDFDocument的引用並將其發佈到dealloc中。在iOS 5之後,您可以在將CGPDFPageRef傳遞給TiledView後立即釋放。爲什麼?不知道。 – ipodishima 2012-07-31 14:16:30

0

首先嚐試做CGPDFDocumentRetain(yourCGPDFDocumentRef)