PDFAnnotationSubType持久化时未保存墨迹(iOS11,PDFKit)

问题描述 投票:2回答:2

在iOS 11中,我有一个PDFView控制器实现,允许注释PDF,如果使用PDFAnnotationSubtypeInk进行自由格式绘制,则使用其中一个注释

        let page : PDFPage = ...
        let points : [CGPoint] = ...
        let path = UIBezierPath()
        for x in 0..<points.count {
            let point = self.pdfView.convert(points[x], to: page)
            if x == 0 {
                path.move(to: point)
            }else{
                path.addLine(to: point)
            }
        }

        let border = PDFBorder()
        border.style = .solid
        border.lineWidth = 2.0

        let drawAnnotation = PDFAnnotation(bounds: page.bounds(for: .mediaBox), forType: .ink, withProperties: nil)
        drawAnnotation.backgroundColor = .yellow
        drawAnnotation.interiorColor = .yellow
        drawAnnotation.fontColor = .yellow
        drawAnnotation.color = .yellow
        drawAnnotation.border = border
        drawAnnotation.add(path)
        page.addAnnotation(drawAnnotation)

当我调用持久性代码时

    if let path = self.pdfDocumentPath, let document = self.pdfDocument {
        if !document.write(toFile: path){
            NSLog("Failed to save PDF")
        }
    }

一切都在理论上有效,PDF保存到磁盘......但是在下一次从磁盘重新加载时,我的注释无处可见。正确保存其他类型的注释(即文本和突出显示)

看起来UIBezierPaths永远不会从磁盘返回。在重新加载的PDFAnnotation中找不到UIBezierPath

难道我做错了什么?我错过了什么吗?

PS:我很抱歉Ruby开发人员正在寻找关于Ruby包的问题/答案,称为PDFKit ... iOS也有一个名为PDFKit的软件包,因为iOS 11 ...对于任何混乱都很抱歉。

swift pdf ios11 pdfkit fastpdfkit
2个回答
2
投票

我在这个问题上遇到了问题。注释被保存,但UIBezierPath没有(以及它的所有点)

因此,当我将PDF写回磁盘时,我会浏览所有页面和所有注释,并找到具有UIBezierPath的那些,并将CGPoints序列化为JSON到PDFAnnotation content属性中。

当重新加载相同的PDF时,我做了完全相同的事情,它遍历所有页面,找到.ink注释,反序列化点,并在注释上添加UIBezierPath。


1
投票

Apple修复了iOS 12中的这个错误.PDFAnnotation.paths填写正确。

从11.2 - 11.4之间的某个版本开始(我没有机会验证,但它在11.1中完全不起作用并且在11.4+中工作)你可以使用PDFAnnotation方法drawWithBox:inContext:正确地渲染墨迹注释。

注意,在iOS 12.0中Apple破坏了PDFAnnotation.color(从磁盘保存重载PDFDocument后,alpha总是等于1)。可以通过上面的方法通过上下文绘制注释来修复。

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