2011-01-09 14 views
5

Sto provando a eseguire il rendering di una CGPDFPage (selezionata da un CGPDFDocument) in una UIImage da visualizzare su una vista.Rendering di una CGPDFPage in una UIImmagine

Ho il seguente codice in MonoTouch che mi porta parzialmente là.

RectangleF PDFRectangle = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height); 

    public override void ViewDidLoad() 
    { 
     UIGraphics.BeginImageContext(new SizeF(PDFRectangle.Width, PDFRectangle.Height)); 
     CGContext context = UIGraphics.GetCurrentContext(); 
     context.SaveState(); 

     CGPDFDocument pdfDoc = CGPDFDocument.FromFile("test.pdf"); 
     CGPDFPage pdfPage = pdfDoc.GetPage(1); 

     context.DrawPDFPage(pdfPage); 
     UIImage testImage = UIGraphics.GetImageFromCurrentImageContext(); 

     pdfDoc.Dispose(); 
     context.RestoreState(); 

     UIImageView imageView = new UIImageView(testImage); 
     UIGraphics.EndImageContext(); 

     View.AddSubview(imageView); 
    } 

Una sezione di CGPDFPage viene visualizzata ma ruotata avanti e indietro e capovolta. La mia domanda è, come posso selezionare la pagina pdf completa e girarla per visualizzarla correttamente. Ho visto alcuni esempi utilizzando ScaleCTM e TranslateCTM ma non sembra che possano farli funzionare.

Eventuali esempi in ObjectiveC vanno bene, mi prendo tutto l'aiuto che posso ottenere :)

Grazie

risposta

13

non ho lavorato con MonoTouch. Tuttavia, nell'obiettivo C si ottiene un'immagine per una pagina PDF come questa (notare le trasformazioni CTM):

-(UIImage *)getThumbForPage:(int)page_number{ 
CGFloat width = 60.0; 

    // Get the page 
CGPDFPageRef myPageRef = CGPDFDocumentGetPage(myDocumentRef, page); 
// Changed this line for the line above which is a generic line 
//CGPDFPageRef page = [self getPage:page_number]; 

CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); 
CGFloat pdfScale = width/pageRect.size.width; 
pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale); 
pageRect.origin = CGPointZero; 


UIGraphicsBeginImageContext(pageRect.size); 

CGContextRef context = UIGraphicsGetCurrentContext(); 

// White BG 
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); 
CGContextFillRect(context,pageRect); 

CGContextSaveGState(context); 

    // *********** 
// Next 3 lines makes the rotations so that the page look in the right direction 
    // *********** 
CGContextTranslateCTM(context, 0.0, pageRect.size.height); 
CGContextScaleCTM(context, 1.0, -1.0); 
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true)); 

CGContextDrawPDFPage(context, page); 
CGContextRestoreGState(context); 

UIImage *thm = UIGraphicsGetImageFromCurrentImageContext(); 

UIGraphicsEndImageContext(); 
return thm; 

} 
+3

Funziona alla grande, grazie! Portato su MonoTouch sembra qualcosa del genere https://gist.github.com/771759 –

+1

Ciao Felz, sto usando il tuo codice per ottenere miniature in formato PDF. Sembra che CGContextConcatCTM (context, CGPDFPageGetDrawingTransform (page, kCGPDFMediaBox, pageRect, 0, true)) produca un collo di bottiglia nella memoria, ho scoperto che se lascio questa linea, allora è OK. Ma non posso lasciarlo perché ho bisogno di questo per un'anteprima corretta. Hai un'idea di come possiamo migliorare questo? Thx btw ... – aslisabanci

+0

Solo una domanda: 'CGPDFPageRef page = [self getPage: page_number];' elimina un errore. In quale tipo di viewController è incorporato il codice? – DAS

Problemi correlati