es.davy.ai

Preguntas y respuestas de programación confiables

¿Tienes una pregunta?

Si tienes alguna pregunta, puedes hacerla a continuación o ingresar lo que estás buscando.

Cómo anotar una imagen redondeada con aspecto de relleno en PDFView (PDFKit swift)

Estoy trabajando en la anotación de una imagen en una PDFView ya existente. Este es mi estado actual:

enter image description here

Y debajo está el estado deseado:

enter image description here

En una frase, quiero lograr:

  1. esquinas redondeadas en mi anotación de imagen
  2. la imagen también debería mantener la relación de aspecto original

He intentado y buscado en muchas fuentes, pero ninguna de ellas funciona.

Este es mi código actual:

class PDFImageAnnotation: PDFAnnotation {

var image: UIImage?

convenience init(_ image: UIImage?, bounds: CGRect, properties: [AnyHashable : Any]?) {
self.init(bounds: bounds, forType: PDFAnnotationSubtype.stamp, withProperties: properties)
self.image = image
}

override func draw(with box: PDFDisplayBox, in context: CGContext) {
super.draw(with: box, in: context)

// Dibujando la imagen dentro de los límites de la anotación.
guard let cgImage = image?.cgImage else { return }
context.draw(cgImage, in: bounds)

}
}

Implementación en ViewDidLoad:

if let image = pet.picture?.getImage() {
let imageAnnotation = PDFImageAnnotation(image, bounds: CGRect(x: 80, y: 1100, width: 300, height: 300), properties: nil)
pdfView.currentPage?.addAnnotation(imageAnnotation)
}

Cualquier sugerencia sería útil porque estoy en un callejón sin salida en este momento, ¡muchas gracias!

Tags:  , , , ,

Answer

  1. Avatar for davy.ai

    Para lograr el estado deseado de una anotación de imagen con esquinas redondeadas y la relación de aspecto original preservada, puede seguir los siguientes pasos:

    1. Use una máscara para recortar la imagen en forma de rectángulo redondeado, como en este ejemplo de código:
    if let image = pet.picture?.getImage() {
        let imageAnnotation = PDFAnnotation(bounds: CGRect(x: 80, y: 1100, width: 300, height: 300), forType: .stamp, withProperties: nil)
        let imageView = UIImageView(image: image)
        imageView.frame = imageAnnotation.bounds
        imageView.contentMode = .scaleAspectFit
        imageView.layer.cornerRadius = imageView.bounds.height / 2
        imageView.layer.masksToBounds = true
        let imageMaskLayer = CAShapeLayer()
        imageMaskLayer.path = UIBezierPath(roundedRect: imageView.bounds, cornerRadius: imageView.layer.cornerRadius).cgPath
        imageView.layer.mask = imageMaskLayer
        UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, false, 0)
        imageView.layer.render(in: UIGraphicsGetCurrentContext()!)
        let roundedImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        imageAnnotation.add(image: roundedImage!)
        pdfView.currentPage?.addAnnotation(imageAnnotation)
    }
    
    1. Use el modo de contenido scaleAspectFit para la vista de imagen para preservar la relación de aspecto mientras se ajusta dentro de los límites de la anotación.

    Tenga en cuenta que este código asume que pet.picture?.getImage() devuelve un objeto UIImage válido. Si devuelve nulo, el código no agregará ninguna anotación a la vista PDF.

Comments are closed.