Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

160
Views
How to resize and reshape UIImage without causing any distortion?

I am using the clearbit api to get and display company logos in my app. This works great except that all of the logos are different size, and I want them all to be displayed as a square.

Does anybody know how to take a rectangular image and add extra pixels to it of a given color until it becomes square with the logo at the center?

This question seemed to answer it, but it is outdated. I also thought that the top answer on this question would work, but the code didn't work. Any help would be very much appreciated, thank you!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

  1. ` 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!
    

    }`

  2. ` 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
    

    }`

over 4 years ago · Santiago Trujillo Report

0

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
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!