gpt4 book ai didi

iOS PDFKit不会写

转载 作者:搜寻专家 更新时间:2023-11-01 06:12:07 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com