How can I make a pdf from UIImage?

I have a UIImage in my UIViewController and I want to transfer this image to PDF, so is it possible for this kind of migration? Please guide me, I do not know about it.

+6
source share
2 answers

If you have a UIImageView in a UIViewController, call this method:

-(NSData*)makePDFfromView:(UIView*)view { NSMutableData *pdfData = [NSMutableData data]; UIGraphicsBeginPDFContextToData(pdfData, view.bounds, nil); UIGraphicsBeginPDFPage(); CGContextRef pdfContext = UIGraphicsGetCurrentContext(); [view.layer renderInContext:pdfContext]; UIGraphicsEndPDFContext(); return pdfData; } 

Use this:

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

It's not very difficult, but there are a few steps to get things going. Basically, you need to create a PDF graphics context and then use it with standard drawing commands. To just put UIImage in a PDF, you can do something like the following:

 // 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(); 

See the Drawing and Printing Guide for iOS for more information.

+9
source

Source: https://habr.com/ru/post/905837/


All Articles