I’m including stamp annotation to PDF file, its working nice after I do not rotate picture however when there’s rotation then annotation is getting clipped from corners. You’ll be able to see this in connected picture.
Right here is the code to create ImageAnnotation
import PDFKit
import UIKit
remaining class ImageAnnotation: PDFAnnotation {
var picture: UIImage
var currentRotationAngle: CGFloat = 0.0
init(bounds: CGRect, picture: UIImage) {
self.picture = picture
tremendous.init(bounds: bounds, forType: .stamp, withProperties: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been applied")
}
override func draw(with field: PDFDisplayBox, in context: CGContext) {
context.saveGState()
let midX = bounds.midX
let midY = bounds.midY
context.translateBy(x: midX, y: midY)
context.rotate(by: -currentRotationAngle)
context.translateBy(x: -midX, y: -midY)
guard let cgImage = picture.cgImage else { return }
context.draw(cgImage, in: bounds)
context.restoreGState()
}
}
That is how I’m including ImageAnnotation sticker to pdf overlay after which changing to annotation when person faucet outdoors of annotation sticker view.
Code so as to add Picture sticker view to PDF overlay
func addImage(_ picture: UIImage) {
var rect = frameForImage(picture, inside: overlayView)
rect.origin.x = overlayView.middle.x - rect.width / 2
rect.origin.y = overlayView.middle.y - rect.peak / 2
self.body = rect
let imageView = UIImageView(picture: picture)
imageView.isUserInteractionEnabled = true
addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.topAnchor.constraint(equalTo: imageView.superview!.topAnchor, constant: 24),
imageView.leadingAnchor.constraint(equalTo: imageView.superview!.leadingAnchor, constant: 24),
imageView.trailingAnchor.constraint(equalTo: imageView.superview!.trailingAnchor, constant: -24),
imageView.bottomAnchor.constraint(equalTo: imageView.superview!.bottomAnchor, constant: -24)
])
}
This methodology add precise annotation to PDFDocument
func addImageAnnotation(picture: UIImage) {
guard let imageView = firstView(kind: UIImageView.self) else {
return
}
let frameAfterMargin = CGRect(x: middle.x - imageView.bounds.width / 2,
y: middle.y - imageView.bounds.peak / 2,
width: imageView.bounds.width,
peak: imageView.bounds.peak)
let viewFrame = overlayView.convert(frameAfterMargin, to: pdfView)
let annotationBounds = pdfView.convert(viewFrame, to: pdfPage)
let annotation = ImageAnnotation(bounds: annotationBounds, picture: picture)
annotation.currentRotationAngle = rotationAngle
pdfPage.addAnnotation(annotation)
}
You’ll be able to entry the pattern undertaking right here to rapidly attempt it out
Pattern Undertaking