无法通过swift使用UIView创建pdf?

时间:2017-08-31 11:41:20

标签: ios objective-c swift xcode pdf

我试图在我的swift应用程序中将自定义UIView保存为pdf,但我失败了。

所以我尝试使用与OC相同的方式,它工作正常,但不是swift。

这些是我的swift和OC的测试代码,它们都可以在模拟器和设备中显示相同的块。

迅速:

@objc class TestViewController: UIViewController {
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        let sub = UIView(frame: CGRect(x: 10, y: 10, width: 100, height: 100))
        sub.backgroundColor = .red
        view.addSubview(sub)

        let data = NSMutableData()
        UIGraphicsBeginPDFContextToData(data, view.bounds, nil)
        UIGraphicsBeginPDFPage()
        view.layer.draw(in: UIGraphicsGetCurrentContext()!)
        UIGraphicsEndPDFContext()

        let path = NSTemporaryDirectory() + "/pdftest_s.pdf"
        data.write(toFile: path, atomically: true)
        print(path)
    }
}

OC:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
@end

@implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UIView *sub = [UIView.alloc initWithFrame:CGRectMake(10, 10, 100, 100)];
    sub.backgroundColor = UIColor.redColor;
    [self.view addSubview:sub];

    NSMutableData *data = [NSMutableData data];
    UIGraphicsBeginPDFContextToData(data, self.view.bounds, nil);
    UIGraphicsBeginPDFPage();
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndPDFContext();

    NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:@"pdftest.pdf"];
    [data writeToFile:path atomically:YES];
    NSLog(@"%@", path);
}
@end

在Xcode 8.3和9.0 beta6中,OC工作正常,但没有swift(3和4)。

我试图使用UIPrintPageRenderer,它不能正常工作!

3 个答案:

答案 0 :(得分:1)

在Obj代码中,您正在渲染view.layer

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

虽然在快速代码中你忘了这样做

用渲染

替换view.layer.draw(in: UIGraphicsGetCurrentContext()!)

希望它有用

答案 1 :(得分:0)

您需要在当前的图形上下文中呈现视图。在swift3中你可以做到

view.layer.render(in: UIGraphicsGetCurrentContext()!)

根据Apple文档

  

renderInContext:将图层及其子图层渲染​​到   指定的背景。

答案 2 :(得分:0)

Swift 4.2中的更新代码:

使用下面的UIView Extension可以轻松地从UIView创建PDF。

extension UIView {

  // Export pdf from Save pdf in drectory and return pdf file path
  func exportAsPdfFromView() -> String {

      let pdfPageFrame = self.bounds
      let pdfData = NSMutableData()
      UIGraphicsBeginPDFContextToData(pdfData, pdfPageFrame, nil)
      UIGraphicsBeginPDFPageWithInfo(pdfPageFrame, nil)
      guard let pdfContext = UIGraphicsGetCurrentContext() else { return "" }
      self.layer.render(in: pdfContext)
      UIGraphicsEndPDFContext()
      return self.saveViewPdf(data: pdfData)

  }

  // Save pdf file in document directory
  func saveViewPdf(data: NSMutableData) -> String {  
    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let docDirectoryPath = paths[0]
    let pdfPath = docDirectoryPath.appendingPathComponent("viewPdf.pdf")
    if data.write(to: pdfPath, atomically: true) {
        return pdfPath.path
    } else {
        return ""
    }
  }
}

信用:http://www.swiftdevcenter.com/create-pdf-from-uiview-wkwebview-and-uitableview/