我是Apples PDF Kit的新手,但我想要达到的目的是用户可以签名表格,以PDF格式提供。我有一个ViewController,我可以在我的pdf中写一些文本,但是一旦我将它从imageView添加到pdf作为注释,它就会被移动和缩放..我想这是代码的重要部分:
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
guard let signatureImage = tempImageView.image, let page = pdfView.currentPage else { return }
let pageBounds = page.bounds(for: .cropBox)
let imageStamp = ImageStampAnnotation(with: signatureImage, forBounds: pageBounds, withProperties: nil)
page.addAnnotation(imageStamp)
tempImageView.image = nil
}
我创建的类,用于将图像添加为PDFAnnotation:
class ImageStampAnnotation: PDFAnnotation {
var image: UIImage!
init(with image: UIImage!, forBounds bounds: CGRect, withProperties properties: [AnyHashable : Any]?) {
super.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp, withProperties: properties)
self.image = image
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(with box: PDFDisplayBox, in context: CGContext) {
guard let cgImage = self.image.cgImage else { return }
context.draw(cgImage, in: self.bounds)
}
}
tempImageView是一个imageview,位于PDFView上。 imageView显示我用手指画出的文字,但是一旦我松开手指,它就会移动并重新缩放..