iOS PDFKit不会写

标签 ios swift pdf

import UIKit
import PDFKit

class ViewController: UIViewController {

    @IBOutlet weak var pdfView: PDFView!

    lazy var pdfDoc:PDFDocument? = {
        guard let path = Bundle.main.path(forResource: "6368", ofType: "pdf") else {return nil}
        let url = URL(fileURLWithPath: path)
        return PDFDocument(url: url)
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupPDFView()
        self.save()
    }

    func setupPDFView() {
        //Setup and put pdf on view
        pdfView.autoScales = true
        pdfView.displayMode = .singlePageContinuous
        pdfView.displayDirection = .horizontal
        pdfView.document = pdfDoc

        self.add(annotation: self.circleAnnotation(), to: 0)
    }

    func add(annotation: PDFAnnotation, to page:Int){
        self.pdfDoc?.page(at: page)?.addAnnotation(annotation)
    }

    func circleAnnotation()->PDFAnnotation {
        let bounds = CGRect(x: 20.0, y: 20.0, width:200.0, height: 200.0)
        let annotation = PDFAnnotation(bounds: bounds, forType: .circle, withProperties: nil)
        annotation.interiorColor = UIColor.black
        return annotation
    }

    func save() {
        //Save to file
        guard let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {return}
        let data = pdfView.document?.dataRepresentation()
        do {
            if data != nil{try data!.write(to: url)}
        } 
        catch {
            print(error.localizedDescription)
        }
    }
}

这只是简单的 PDFKit 代码,应该在 pdf 的第一页上添加一个圆圈并将其保存到文档目录中。所有作品,除了保存。当我在 let data = .. 之后断点时。显示有数据,但是有两个错误:

1) 2018-12-18 05:10:28.887195-0500 PDFWriteTest[21577:1331197] [Unknown process name] Failed to load /System/Library/PrivateFrameworks/CorePDF.framework/Versions/A/CorePDF

  • 不管我是否发现错误并打印,这个都会显示

2) The file “Documents” couldn’t be saved in the folder “D3E23B05-92...”.

  • 这是从 error.localizedDescription 打印的内容

可以使用 PDFKit 解决这个问题(将 PDF 数据保存到文件)吗?

最佳答案

您不能将数据直接写入表示 Documents 文件夹的 URL,您必须指定(并附加)一个文件名。

func save(filename : String) {
    //Save to file
    guard let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first,
          let data = pdfView.document?.dataRepresentation() else {return}
    let fileURL = url.appendingPathComponent(filename)
    do {
        try data.write(to: fileURL)
    } catch {
        print(error.localizedDescription)
    }
}

关于iOS PDFKit不会写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53831075/

相关文章:

ios - UILabel 文本被截断

ios - 在 swift 2.0 中暂停 mp3 文件

matlab - MATLAB 的 PDF 输出并包含在 LaTeX 中

ios - 在 iOS 上,如果 drawRect 不使用传入的矩形,那么 [self setNeedsDisplayInRect :rect] passes in a rect? 是否重要

ios - 在 ARC 中将自己分配给 __weak 时应用程序崩溃?

ios - Pod Spec Lint CoreSimulator 检测到 Xcode.app 重定位或 CoreSimulatorService 版本更改

ios - 有没有更好的方法从字典中读取元素?

ios - 无法使用 swift 从 json 访问嵌套字典/数组

javascript - FPDF - 使用 PDF_JS 时在没有对话框的情况下打印

java - 使用 Apache PDFBox 生成 unicode pdf