Ho un UIImage nel mio UIViewController e voglio migrare quell'immagine in formato PDF così è possibile questo tipo di migrazione? Per favore guidami non ne ho idea.Come posso creare Pdf da UIImage?
6
A
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.