首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在不引起任何失真的情况下调整和重塑UIImage?

如何在不引起任何失真的情况下调整和重塑UIImage?
EN

Stack Overflow用户
提问于 2022-02-04 05:56:40
回答 2查看 327关注 0票数 3

我正在使用clearbit api来获取和显示我的应用程序中的公司标识。这是伟大的工作,除了所有的标志是不同的大小,我希望他们所有的显示为一个正方形。

有没有人知道如何取一个矩形的图像,并添加额外的像素,一个给定的颜色,直到它变成方形与标志在中心?

This question似乎回答了这个问题,但它已经过时了。我还认为在this question上的最高答案是可行的,但是代码不起作用。任何帮助都是非常感谢的,谢谢!

EN

回答 2

Stack Overflow用户

发布于 2022-02-11 10:49:41

代码语言:javascript
运行
复制
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
    }
}
票数 1
EN

Stack Overflow用户

发布于 2022-02-08 08:33:55

  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! }

  1. 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 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70982018

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档