gpt4 book ai didi

iOS swift : How to properly scale down an image?

转载 作者:可可西里 更新时间:2023-11-01 00:22:37 24 4
gpt4 key购买 nike

我正在使用 AlamofireImage 下载并设置我的 UIImage:

backdrop.af_setImageWithURL(downloadURL)

图像比我要显示的要大得多,所以我遇到了锯齿问题。如何正确调整此图像的大小?

结果:

enter image description here

最佳答案

一旦你有了一个有效的UIImage,你就可以调整任何大小的图像:

func resizedImageWith(image: UIImage, targetSize: CGSize) -> UIImage {

let imageSize = image.size
let newWidth = targetSize.width / image.size.width
let newHeight = targetSize.height / image.size.height
var newSize: CGSize

if(newWidth > newHeight) {
newSize = CGSizeMake(imageSize.width * newHeight, imageSize.height * newHeight)
} else {
newSize = CGSizeMake(imageSize.width * newWidth, imageSize.height * newWidth)
}

let rect = CGRectMake(0, 0, newSize.width, newSize.height)

UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)

image.drawInRect(rect)

let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

return newImage
}

对于任何其他查询/修改,请参阅此 NShipster

关于iOS swift : How to properly scale down an image?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38062289/

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