我需要缩放来自 iPhone 应用程序中视图层的图像的分辨率。显而易见的方法是在 UIGraphicsBeginImageContextWithOptions 中指定比例因子,但只要比例因子不是 1.0,图像质量就会受到影响——远远超出像素损失的预期。
我尝试过其他几种缩放技术,但它们似乎都围绕 CGContext 的东西,并且似乎都做同样的事情。
仅仅改变图像“大小”(不改变点分辨率)是不够的,主要是因为该信息似乎很快就被管道中的其他人丢弃(图像将转换为 JPG 并通过电子邮件发送)。
iPhone 上还有其他缩放图像的方法吗?
快速扩展:
extension UIImage{
// returns a scaled version of the image
func imageScaledToSize(size : CGSize, isOpaque : Bool) -> UIImage{
// begin a context of the desired size
UIGraphicsBeginImageContextWithOptions(size, isOpaque, 0.0)
// draw image in the rect with zero origin and size of the context
let imageRect = CGRect(origin: CGPointZero, size: size)
self.drawInRect(imageRect)
// get the scaled image, close the context and return the image
let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaledImage
}
}
Example:
aUIImageView.image = aUIImage.imageScaledToSize(aUIImageView.bounds.size, isOpaque : false)
如果图像没有 alpha,则将 isOpaque 设置为 true:绘图将具有更好的性能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)