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

4

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

1个回答

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)))
    }

}

谢谢您的回复,实际上我已经找到了解决方案,只是忘记发布,我会接受它。 - newszer
你的答案非常出色。最后的小技巧是在PDF被调整大小或裁剪时使印章出现在原始边界内的唯一方法。谢谢! - Ron Regev
完美运行! - liudasbar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接