gpt4 book ai didi

ios - 调整图像大小而不扭曲或裁剪它

转载 作者:搜寻专家 更新时间:2023-10-31 21:46:53 24 4
gpt4 key购买 nike

用户上传任意尺寸的图片,我们需要调整它的大小,使其变成正方形,而不扭曲或裁剪图片。基本上,它应该做一些类似于 ImageView 中的“Aspect Fit”内容模式的事情。因此,如果我们有一个 200x100px 的 png 图像,我想将其设置为 200x200px,并在高度上增加 100px 成为透明空间。它不应将图像裁剪为 200x200。

我尝试使用此图像处理器,但它无法满足我的要求。 https://github.com/gavinbunney/Toucan .它只裁剪图像。

我将如何快速完成此操作,是否有比我上面提到的框架更好的框架来简化此操作。基本上,我正在寻找最简单的方法来做到这一点。

最佳答案

将此作为答案以及示例用法发布...

缩放代码不是我的,它来自:https://gist.github.com/tomasbasham/10533743#gistcomment-1988471

这是您可以在 Playground 上运行以进行测试的代码:

import UIKit
import PlaygroundSupport

let container = UIView(frame: CGRect(x: 0, y: 0, width: 800, height: 800))

container.backgroundColor = UIColor.blue

PlaygroundPage.current.liveView = container


// MARK: - Image Scaling.
extension UIImage {

/// Represents a scaling mode
enum ScalingMode {
case aspectFill
case aspectFit

/// Calculates the aspect ratio between two sizes
///
/// - parameters:
/// - size: the first size used to calculate the ratio
/// - otherSize: the second size used to calculate the ratio
///
/// - return: the aspect ratio between the two sizes
func aspectRatio(between size: CGSize, and otherSize: CGSize) -> CGFloat {
let aspectWidth = size.width/otherSize.width
let aspectHeight = size.height/otherSize.height

switch self {
case .aspectFill:
return max(aspectWidth, aspectHeight)
case .aspectFit:
return min(aspectWidth, aspectHeight)
}
}
}

/// Scales an image to fit within a bounds with a size governed by the passed size. Also keeps the aspect ratio.
///
/// - parameter:
/// - newSize: the size of the bounds the image must fit within.
/// - scalingMode: the desired scaling mode
///
/// - returns: a new scaled image.
func scaled(to newSize: CGSize, scalingMode: UIImage.ScalingMode = .aspectFill) -> UIImage {

let aspectRatio = scalingMode.aspectRatio(between: newSize, and: size)

/* Build the rectangle representing the area to be drawn */
var scaledImageRect = CGRect.zero

scaledImageRect.size.width = size.width * aspectRatio
scaledImageRect.size.height = size.height * aspectRatio
scaledImageRect.origin.x = (newSize.width - size.width * aspectRatio) / 2.0
scaledImageRect.origin.y = (newSize.height - size.height * aspectRatio) / 2.0

/* Draw and retrieve the scaled image */
UIGraphicsBeginImageContext(newSize)

draw(in: scaledImageRect)
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return scaledImage!
}
}

if let srcimg = UIImage(named: "flags") {

let w = srcimg.size.width
let h = srcimg.size.height

// determine whether width or height is greater
let longer = max(w, h)

// create a Square size
let sz = CGSize(width: longer, height: longer)

// call scaling function to scale the image to the Square dimensions,
// using "aspect fit"
let newImage = srcimg.scaled(to: sz, scalingMode: .aspectFit)

// create a UIImageView with the resulting image
let v = UIImageView(image: newImage)
v.backgroundColor = UIColor.white

// add it to the container view
container.addSubview(v)

}

关于ios - 调整图像大小而不扭曲或裁剪它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44416827/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com