Get a PDF/PNG as output from a UIWebView or UIView

后端 未结 4 2156
傲寒
傲寒 2020-12-02 12:22

Is there any way to get the content of a UIWebView and convert it to a PDF or PNG file? I\'d like to get similar output to that available on the Mac by selectin

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 13:08

    @mjdth, try fileURLWithPath:isDirectory: instead. URLWithString wasn't working for me either.

    @implementation UIView(PDFWritingAdditions)
    
    - (void)renderInPDFFile:(NSString*)path
    {
        CGRect mediaBox = self.bounds;
        CGContextRef ctx = CGPDFContextCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path isDirectory:NO], &mediaBox, NULL);
    
        CGPDFContextBeginPage(ctx, NULL);
        CGContextScaleCTM(ctx, 1, -1);
        CGContextTranslateCTM(ctx, 0, -mediaBox.size.height);
        [self.layer renderInContext:ctx];
        CGPDFContextEndPage(ctx);
        CFRelease(ctx);
    }
    
    @end
    

提交回复
热议问题