gpt4 book ai didi

ios - 将 UIImage 从矩形调整为正方形而不失真

转载 作者:行者123 更新时间:2023-11-29 02:40:46 26 4
gpt4 key购买 nike

我想创建一个函数,将图像大小从矩形调整为正方形而不失真。

这是我的代码:我有一个错误(表达式不符合类型“$T4”): if (UIScreen.mainScreen().respondsToSelector(aSelector:scaleTransform))

func squareImageFromImage(image:UIImage, newSize: CGFloat) -> UIImage {
var scaleTransform :CGAffineTransform
var origin : CGPoint
var scaleRatio: CGFloat

if (image.size.width > image.size.height) {
scaleRatio = newSize / image.size.height
scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);

origin = CGPointMake(-(image.size.width - image.size.height) / 2.0, 0)
} else {
scaleRatio = newSize / image.size.width
scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);

origin = CGPointMake(0, -(image.size.height - image.size.width) / 2.0)
}

var size: CGSize = CGSizeMake(newSize, newSize)

if (UIScreen.mainScreen().respondsToSelector(aSelector: scaleTransform)) {

UIGraphicsBeginImageContextWithOptions(size, false, 0.0)

} else {

UIGraphicsBeginImageContext(size)
}

var context: CGContextRef = UIGraphicsGetCurrentContext()
CGContextConcatCTM(context, scaleTransform)

image.drawAtPoint(origin)

var newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()

UIGraphicsEndImageContext()

return newImage
}

最佳答案

下面的代码也适用于 Swift 2.3 和 3.0

UIScreen.mainScreen().respondsToSelector(#selector(NSDecimalNumberBehaviors.scale))

关于ios - 将 UIImage 从矩形调整为正方形而不失真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25832150/

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