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

问题描述 投票:0回答:3

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

因此我尝试使用与OC相同的方式,它工作正常,但不是很快。

这些是我的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,它不工作!

ios objective-c swift xcode pdf
3个回答
1
投票

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

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

在快速的代码中,你忘了这样做

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

希望它有所帮助


0
投票

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

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

根据Apple文档

renderInContext:将图层及其子图层渲染​​到指定的上下文中。


0
投票

更新了Swift 4.2中的代码:

使用UIView扩展下方可以轻松地从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/

© www.soinside.com 2019 - 2024. All rights reserved.