
Вот код для создания ImageAnnotation
Код: Выделить всё
import PDFKit
import UIKit
final class ImageAnnotation: PDFAnnotation {
var image: UIImage
var currentRotationAngle: CGFloat = 0.0
init(bounds: CGRect, image: UIImage) {
self.image = image
super.init(bounds: bounds, forType: .stamp, withProperties: nil)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func draw(with box: 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 = image.cgImage else { return }
context.draw(cgImage, in: bounds)
context.restoreGState()
}
}
Код для добавления Просмотр наклейки с изображением в формате PDF
Код: Выделить всё
func addImage(_ image: UIImage) {
var rect = frameForImage(image, within: overlayView)
rect.origin.x = overlayView.center.x - rect.width / 2
rect.origin.y = overlayView.center.y - rect.height / 2
self.frame = rect
let imageView = UIImageView(image: image)
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)
])
}
Код: Выделить всё
func addImageAnnotation(image: UIImage) {
guard let imageView = firstView(type: UIImageView.self) else {
return
}
let frameAfterMargin = CGRect(x: center.x - imageView.bounds.width / 2,
y: center.y - imageView.bounds.height / 2,
width: imageView.bounds.width,
height: imageView.bounds.height)
let viewFrame = overlayView.convert(frameAfterMargin, to: pdfView)
let annotationBounds = pdfView.convert(viewFrame, to: pdfPage)
let annotation = ImageAnnotation(bounds: annotationBounds, image: image)
annotation.currentRotationAngle = rotationAngle
pdfPage.addAnnotation(annotation)
}
Пример проекта
Подробнее здесь: https://stackoverflow.com/questions/793 ... ios-pdfkit