iOS Swift:如何正确缩小图像? [英] iOS Swift: How to properly scale down an image?
本文介绍了iOS Swift:如何正确缩小图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用AlamofireImage下载并设置我的UIImage:
I'm using AlamofireImage to download and set my UIImage:
backdrop.af_setImageWithURL(downloadURL)
图像比我要显示的图像大得多,所以我遇到了别名问题。如何正确调整此图像的大小?
The image is substantially larger than what I will be displaying and so I have an issue with aliasing. How can I resize this image properly?
结果:
推荐答案
一旦有了有效的 UIImage $,你可以用任何尺寸调整图像大小c $ c>:
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
For any other queries/modifications you have, refer this NShipster
这篇关于iOS Swift:如何正确缩小图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文