如何从 UIImage 制作 Pdf?

发布于 2024-12-26 11:21:29 字数 81 浏览 1 评论 0原文

我的 UIViewController 中有一个 UIImage,我想将该图像迁移到 PDF 格式,那么这种迁移是否可能?请指导我,我对此一无所知。

I have one UIImage in my UIViewController and I want to migrate that image to PDF formate so is it possible to such kind of migration?Please guide me i don't have idea of this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傾城如夢未必闌珊 2025-01-02 11:21:29

如果你有 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;
}

像这样使用:

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

If you have UIImageView into UIViewController then 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 like this:

NSData *pdfData = [self makePDFfromView:imgView];
//[pdfData writeToFile:@"myPdf.pdf" atomically:YES]; - save it to a file
无风消散 2025-01-02 11:21:29

这并不难,但需要几个步骤来设置所有内容。基本上,您需要创建一个 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();

有关详细信息,请参阅 iOS 绘图和打印指南

It's not very hard, but there are a few steps to set everything up. Basically, you need to create a PDF graphics context, and then draw into it with standard drawing commands. To simply put an UIImage into a PDF, you could 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();

For more information, see the Drawing and Printing Guide for iOS.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文