Estoy usando la API de clearbit para obtener y mostrar los logotipos de la empresa en mi aplicación. Esto funciona muy bien, excepto que todos los logotipos tienen diferentes tamaños y quiero que todos se muestren como un cuadrado.
¿Alguien sabe cómo tomar una imagen rectangular y agregarle píxeles adicionales de un color determinado hasta que se convierta en un cuadrado con el logotipo en el centro?
Esta pregunta parecía responderla, pero está desactualizada. También pensé que la respuesta principal a esta pregunta funcionaría, pero el código no funcionó. Cualquier ayuda sería muy apreciada, ¡gracias!
` func drawImageOnCanvas(_ useImage: UIImage, canvasColor: UIColor ) -> UIImage {
let length = max(useImage.size.width, useImage.size.height) let canvasSize = CGSize.init(width: length, height: length) let rect = CGRect(origin: .zero, size: canvasSize) UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0) // fill the entire image canvasColor.setFill() UIRectFill(rect) // calculate a Rect the size of the image to draw, centered in the canvas rect let centeredImageRect = CGRect(x: (canvasSize.width - useImage.size.width) / 2, y: (canvasSize.height - useImage.size.height) / 2, width: useImage.size.width, height: useImage.size.height) // get a drawing context let context = UIGraphicsGetCurrentContext(); // "cut" a transparent rectanlge in the middle of the "canvas" image context?.clear(centeredImageRect) // draw the image into that rect useImage.draw(in: centeredImageRect) // get the new "image in the center of a canvas image" let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image!}`
` func imageWithColor(color : UIColor,oriImage:UIImage) -> UIImage{
let length = max(oriImage.size.width, oriImage.size.height) let size = CGSize.init(width: length, height: length) let imgView = UIImageView.init(image: oriImage) imgView.contentMode = UIView.ContentMode.scaleAspectFit imgView.backgroundColor = color imgView.bounds = CGRect.init(x: 0, y: 0, width: length, height: length) print("\(size.width)" + "\(size.height)") UIGraphicsBeginImageContext(size) guard let context = UIGraphicsGetCurrentContext() else{ print("context get Error") return oriImage } imgView.layer.render(in:context) let desImg = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return desImg ?? oriImage}`
import UIKit extension UIImage { func scalePreservingAspectRatio(targetSize: CGSize) -> UIImage { // Determine the scale factor that preserves aspect ratio let widthRatio = targetSize.width / size.width let heightRatio = targetSize.height / size.height let scaleFactor = min(widthRatio, heightRatio) // Compute the new image size that preserves aspect ratio let scaledImageSize = CGSize( width: size.width * scaleFactor, height: size.height * scaleFactor ) // Draw and return the resized UIImage let renderer = UIGraphicsImageRenderer( size: scaledImageSize ) let scaledImage = renderer.image { _ in self.draw(in: CGRect( origin: .zero, size: scaledImageSize )) } return scaledImage } }