iOS11中如何在PDFKit中添加图像注释

new*_*zer 4 pdf annotations ios11

我正在iOS11中使用PDFKit开发一个可以显示pdf文件添加注释的应用程序。现在我想向pdf页面添加图像注释,我认为注释子类型应该是stamp。但在 PDFKit 中我找不到像“ image”这样的属性来设置,唯一与图章注释相关的属性是stampName。有人可以帮忙吗?

小智 5

这是我成功做到这一点的方法。这个想法是重写draw()函数来执行现在不存在的PDFAnnotationStamp类的投标(我发现了一些关于现在已经消失的不同类型的PDFAnnotation子类的过时文档)。这个问题似乎不是最近才出现的,而且开发板的 iOS 端确实是空的,这几乎是可怕的。

public class ImageAnnotation: PDFAnnotation {

    private var _image: UIImage?

    public init(imageBounds: CGRect, image: UIImage?) {
        self._image = image
        super.init(bounds: imageBounds, forType: .stamp, withProperties: nil)
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override public func draw(with box: PDFDisplayBox, in context: CGContext) {
        guard let cgImage = self._image?.cgImage else {
            return
        }
       let drawingBox = self.page?.bounds(for: box)
       //Virtually changing reference frame since the context is agnostic of them. Necessary hack.
       context.draw(cgImage, in: self.bounds.applying(CGAffineTransform(
       translationX: (drawingBox?.origin.x)! * -1.0, 
                  y: (drawingBox?.origin.y)! * -1.0)))
    }

}
Run Code Online (Sandbox Code Playgroud)