Как точно позиционировать изображения в PDF с помощью Swift - PullRequest
0 голосов
/ 12 июля 2020

Я создаю приложение для подписи PDF-документов. В настоящее время я использую подход, при котором вы подписываетесь в представлении подписи, а затем ваша подпись преобразуется в изображение. Затем вы можете изменить размер и повернуть это изображение подписи в PDF. Но при добавлении изображения в качестве аннотации с ImageStampAnnotation для PDFView изображение перемещается в совершенно другое место, чем указывает пользователь.

Вот класс ImageStampAnnotation

class ImageStampAnnotation: PDFAnnotation {

var image: UIImage!



// A custom init that sets the type to Stamp on default and assigns our Image variable
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) {
    
    // Get the CGImage of our image
    guard let cgImage = self.image.cgImage else { return }
    
    // Draw our CGImage in the context of our PDFAnnotation bounds
    context.draw(cgImage, in: self.bounds)
    
}

}

Я добавляю изображение в качестве подпредставления в PDFView до того, как пользователь сможет панорамировать изображение в pdf

func spawnImage(imageVar: UIImage) -> UIImageView {
    let imageView = SOXPanRotateZoomImageView(image: imageVar)
    //let page = pdfContainerView.currentPage
    imageView.center = self.view.center
    //pdfContainerView.documentView?.addSubview(imageView)
    pdfContainerView.addSubview(imageView)
    imageView.backgroundColor = .lightGray
    imageView.alpha = 0.5
    return imageView
}

И вот как я добавляю изображение в pdf с ImageStampAnnotation после пользователя выполнено панорамирование

@IBAction func btnDoneToolbar(_ sender: Any) {

isDocumentSigned = true
toolbarColorVc.isHidden = true
guard let page = pdfContainerView.currentPage else { return }
let imageFrame =
    pdfContainerView.convert(pdfSignatureImageView!.frame, from: page)
//let imageFrame = pdfSignatureImageView?.frame
let imageBounds = CGRect(x: (imageFrame.origin.x), y: (imageFrame.origin.y), width: (pdfSignatureImageView?.frame.size.width)!, height: (pdfSignatureImageView?.frame.size.height)!)
pdfSignatureImageView?.backgroundColor = .clear
let imageStamp = ImageStampAnnotation(with: pdfSignatureImageView?.image, forBounds: imageBounds, withProperties: nil)

page.addAnnotation(imageStamp)
navigationItem.hidesBackButton = false
pdfSignatureImageView?.isHidden = true
isNavButtonsEnabled(true)

}

Вот результат

Вот репо для класса, который я использую для панорамирования

This is the position of the image specified by the user after panning Это позиция подписи на pdf

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...