菜鸟教程小白 发表于 2022-12-12 10:49:27

ios - 在 Web View 中生成 PDF 时,如何首先将 PDF 数据写入文件


                                            <p><p>我的应用在生成 PDF 时不断收到内存警告和随后的崩溃。有人建议我先将我的 PDF 写入文件,但没有提供扩展。</p>

<p>这是我第一次使用 iOS 生成 PDF,尽管查看了 Apple 文档 <a href="http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html" rel="noreferrer noopener nofollow">http://developer.apple.com/library/ios/#documentation/2ddrawing/conceptual/drawingprintingios/GeneratingPDF/GeneratingPDF.html</a>并搜索 SO、Google 并使用 <code>UIGraphicsBeginPDFContextToFile</code> 我只是不断收到警告/没有生成 PDF 或抛出异常,因为我只是在猜测。这个概念我还真没搞懂。 </p>

<p>有人可以提供一些关于如何首先将我的 PDF 写入文件的建议</p>

<pre><code>- (void)viewWillAppear:(BOOL)animated
{
LogCmd();

;
if (self.pdfData != nil &amp;&amp; self.viewHasUnloaded == YES) {
self.viewHasUnloaded = NO;
;
}
}



- (void)viewDidAppear:(BOOL)animated
{

LogCmd();
;
if (self.pdfData == nil) {



// Generate PDF
;
;
}
}

- (void)generatePdf
{
NSMutableArray *pagesArray = ;

if () {
initWithCertificate:self.certificate]];
initWithCertificate:self.certificate]];
;
;
initWithCertificate:self.certificate]];
initWithCertificate:self.certificate]];
initWithCertificate:self.certificate]];
initWithCertificate:self.certificate]];
initWithCertificate:self.certificate]];
    }

    // Set page count on all pages
   int pageNumber = 0;
   for (ICCertificateComponent *page in pagesArray) {
   page.pageNumber.text = ;
   page.pageCount.text = ;
   }

   NSData *pdfData = ;
   ;

}
}
- (void)pdfDone:(NSData *)data
{
self.pdfData = data;
;
;
      }


- (NSData *)createPdfWithPages:(NSArray *)pages

   {
    // Creates a mutable data object for updating with binary data, like a byte array

NSMutableData *pdfData = ;

ICCertificateComponent *firstPage = ;

UIGraphicsBeginPDFContextToData(pdfData, firstPage.contentView.bounds, nil);

for (int i = 0; i &lt; pages.count; i++) {
   ICCertificateComponent *thisPage = ;
UIGraphicsBeginPDFPageWithInfo(thisPage.contentView.bounds, nil);

CGContextRef pdfContext = UIGraphicsGetCurrentContext();
;
   }

UIGraphicsEndPDFContext();

return pdfData;

}
</code></pre>

<p><strong>我的尝试</strong></p>

<pre><code> -(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName:(NSString*)aFilename
{

NSArray *pages;
// Creates a mutable data object for updating with binary data, like a byte array
      NSMutableData *pdfData = ;

    ICCertificateComponent *firstPage = ;

    UIGraphicsBeginPDFContextToData(pdfData, firstPage.contentView.bounds, nil);

    for (int i = 0; i &lt; pages.count; i++) {
      ICCertificateComponent *thisPage = ;
      UIGraphicsBeginPDFPageWithInfo(thisPage.contentView.bounds, nil);

      CGContextRef pdfContext = UIGraphicsGetCurrentContext();
      ;
      }

    UIGraphicsBeginPDFPage();
   CGContextRef currentContext = UIGraphicsGetCurrentContext();
animated:NO];
;



UIGraphicsEndPDFContext();

   NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

   NSString* documentDirectory = ;
   NSString* documentDirectoryFilename = ;

//return pdfData;
;
}
</code></pre>

<p><strong>错误:</strong>抛出异常
<code>'NSInvalidArgumentException',原因:'-: unrecognized selector sent to instance</code> 其中我确定</p>

<p><strong>也试过了:</strong></p>

<pre><code> - (NSString *)createPdfWithPages:(NSArray *)pages
{

    @autoreleasepool {



    ICCertificateComponent *firstPage = ;

    NSString *docDirectory =
    ;
    NSString *pdfPath = ;

    UIGraphicsBeginPDFContextToFile(pdfPath, firstPage.contentView.bounds, nil);

    for (int i = 0; i &lt; pages.count; i++) {
      ICCertificateComponent *thisPage = ;
      UIGraphicsBeginPDFPageWithInfo(thisPage.contentView.bounds, nil);

      CGContextRef pdfContext = UIGraphicsGetCurrentContext();
      ;
               }

    UIGraphicsEndPDFContext();

    return pdfPath;

}
</code></pre>

<p>错误:<code>NSInvalidArgumentException',原因:'-: unrecognized selector sent to instance 0x24aa4580'</code></p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>你试试下面的代码。</p>

<pre><code>+(void)drawLineFromPoint:(CGPoint)from toPoint:(CGPoint)to
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 2.0);

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

    CGFloat components[] = {0.2, 0.2, 0.2, 0.3};

    CGColorRef color = CGColorCreate(colorspace, components);

    CGContextSetStrokeColorWithColor(context, color);

    CGContextMoveToPoint(context, from.x, from.y);
    CGContextAddLineToPoint(context, to.x, to.y);

    CGContextStrokePath(context);
    CGColorSpaceRelease(colorspace);
    CGColorRelease(color);

}

+(void)drawText
{

    NSString* textToDraw = @&#34;Hello World&#34;;
    CFStringRef stringRef = (__bridge CFStringRef)textToDraw;
    // Prepare the text using a Core Text Framesetter
    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL, stringRef, NULL);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);

    CGRect frameRect = CGRectMake(0, 0, 300, 50);
    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, frameRect);

    // Get the frame that will do the rendering.
    CFRange currentRange = CFRangeMake(0, 0);
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);

    // Get the graphics context.
    CGContextRef currentContext = UIGraphicsGetCurrentContext();

    // Put the text matrix into a known state. This ensures
    // that no old scaling factors are left in place.
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

    // Core Text draws from the bottom-left corner up, so flip
    // the current transform prior to drawing.
    CGContextTranslateCTM(currentContext, 0, 100);
    CGContextScaleCTM(currentContext, 1.0, -1.0);

    // Draw the frame.
    CTFrameDraw(frameRef, currentContext);

    CFRelease(frameRef);
    CFRelease(stringRef);
    CFRelease(framesetter);
}

+(void)drawPDF:(NSString*)fileName
{

    // Create the PDF context using the default page size of 612 x 792.
    UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
    // Mark the beginning of a new page.
    UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

    CGPoint from = CGPointMake(0, 0);
    CGPoint to = CGPointMake(200, 300);
    ;

    ;

    // Close the PDF context and write the contents out.
    UIGraphicsEndPDFContext();
}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 在 WebView 中生成 PDF 时,如何首先将 PDF 数据写入文件,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/16416206/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/16416206/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 在 Web View 中生成 PDF 时,如何首先将 PDF 数据写入文件