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:
Y debajo está el estado deseado:
En una frase, quiero lograr:
- esquinas redondeadas en mi anotación de imagen
- 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!
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:
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.