我一直在使用iOS PDFKit
框架处理PDF编辑器/注释器。当我使用默认选项创建注释时,它们最终会奇怪地呈现或者根本不呈现(我已经确定它们意味着它们只是在注释的边界之外进行渲染,因为设置注释边界说{0,0,4000,4000}
有效) 。现在显而易见的是,我正在设置错误的边界,但是当我继承PDFAnnotation
并覆盖draw(with:,in:)
方法时,它们会按预期显示。
不起作用
// paths made using pdf coordinate space
let paths: [UIBezierPath] = []
// find bounding box of all the paths
let bounds = paths.reduce(CGMutablePath(), { acc, nxt in
acc.addPath(nxt.cgPath)
return acc
}).boundingBoxOfPath.insetBy(dx: -50, dy: -50)
let border = PDFBorder()
border.lineWidth = 2.5
border.style = .solid
annotation = PDFAnnotation(
bounds: bounds,
forType: .ink,
withProperties: [
PDFAnnotationKey.inklist: paths,
PDFAnnotationKey.border: border,
PDFAnnotationKey.color: UIColor.red,
PDFAnnotationKey.interiorColor: UIColor.red,
])
// Also tried these things / other fiddling but same results
annotation.backgroundColor = UIColor.red
annotation.shouldDisplay = true
for path in paths {
annotation.add(path)
}
效果很好,但不符合pdf规范
let paths: [UIBezierPath] = []
let bounds = paths.reduce(CGMutablePath(), { acc, nxt in
acc.addPath(nxt.cgPath)
return acc
}).boundingBoxOfPath.insetBy(dx: -50, dy: -50)
annotation = InkAnnotation(bounds: bounds, paths: paths)
/// Overrides draw method to add paths manually
class InkAnnotation: PDFAnnotation {
var pathList: [UIBezierPath] = []
public init(bounds: CGRect, paths: [UIBezierPath]) {
self.pathList = paths
super.init(bounds: bounds, forType: .ink, withProperties: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(with box: PDFDisplayBox, in context: CGContext) {
UIGraphicsPushContext(context)
context.saveGState()
context.setStrokeColor(UIColor.red.cgColor)
context.setLineWidth(2.5)
for path in pathList {
context.addPath(path.cgPath)
context.strokePath()
}
context.strokePath()
context.restoreGState()
UIGraphicsPopContext()
}
}
非常感谢任何帮助!