2012-01-12 21 views

risposta

10

Se hai UIImageView in UIViewController quindi chiamare questo metodo:

-(NSData*)makePDFfromView:(UIView*)view 
{ 
    NSMutableData *pdfData = [NSMutableData data]; 

    UIGraphicsBeginPDFContextToData(pdfData, view.bounds, nil); 
    UIGraphicsBeginPDFPage(); 
    CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 
    [view.layer renderInContext:pdfContext]; 
    UIGraphicsEndPDFContext(); 

    return pdfData; 
} 

Usa come questo:

NSData *pdfData = [self makePDFfromView:imgView]; 
//[pdfData writeToFile:@"myPdf.pdf" atomically:YES]; - save it to a file 
9

Non è molto difficile, ma ci sono alcuni passaggi per impostare tutto. Fondamentalmente, è necessario creare un contesto grafico PDF e quindi disegnare con comandi di disegno standard. Per dirla semplicemente un UIImage in un PDF, si potrebbe fare qualcosa di simile al seguente:

// assume this exists and is in some writable place, like Documents 
NSString* pdfFilename = /* some pathname */ 

// Create the PDF context 
UIGraphicsBeginPDFContextToFile(pdfFilename, CGRectZero, nil); // default page size 
UIGraphicsBeginPDFPageWithInfo(CGRectZero, nil); 

// Draw the UIImage -- I think PDF contexts are flipped, so you may have to 
// set a transform -- see the documentation link below if your image draws 
// upside down 
[theImage drawAtPoint:CGPointZero]; 

// Ending the context will automatically save the PDF file to the filename 
UIGraphicsEndPDFContext(); 

Per ulteriori informazioni, vedere la Drawing and Printing Guide for iOS.