我如何从UIImage制作Pdf?

Ank*_*yas 6 pdf uiimage ios

我的UIViewController中有一个UIImage,我想将该图像迁移到PDF格式,所以有可能进行这种迁移吗?请指导我,我不知道这个.

ber*_*ium 10

如果您将UIImageView放入UIViewController,则调用此方法:

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

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

    return pdfData;
}
Run Code Online (Sandbox Code Playgroud)

使用这样:

NSData *pdfData = [self makePDFfromView:imgView];
//[pdfData writeToFile:@"myPdf.pdf" atomically:YES]; - save it to a file
Run Code Online (Sandbox Code Playgroud)


Jas*_*oco 9

这不是很难,但有一些步骤来设置一切.基本上,您需要创建PDF图形上下文,然后使用标准绘图命令绘制它.要简单地将UIImage放入PDF,您可以执行以下操作:

// 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();
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅适用于iOS绘图和打印指南.