我正在用这个功能调整各种图像的大小:
func resizeImage(image: UIImage) -> UIImage {
var actualHeight: CGFloat = image.size.height
var actualWidth: CGFloat = image.size.width
let maxHeight: CGFloat = 600.0
let maxWidth: CGFloat = text.frame.size.width - 10
var imgRatio: CGFloat = actualWidth / actualHeight
let maxRatio: CGFloat = maxWidth / maxHeight
let compressionQuality: CGFloat = 0.5
//50 percent compression
if actualHeight > maxHeight || actualWidth > maxWidth {
if imgRatio < maxRatio {
//adjust width according to maxHeight
imgRatio = maxHeight / actualHeight
actualWidth = imgRatio * actualWidth
actualHeight = maxHeight
}
else if imgRatio > maxRatio {
//adjust height according to maxWidth
imgRatio = maxWidth / actualWidth
actualHeight = imgRatio * actualHeight
actualWidth = maxWidth
}
else {
actualHeight = maxHeight
actualWidth = maxWidth
}
}
let rect: CGRect = CGRectMake(0.0, 0.0, actualWidth, actualHeight)
UIGraphicsBeginImageContext(rect.size)
image.drawInRect(rect)
let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
let imageData: NSData = UIImageJPEGRepresentation(img, compressionQuality)!
UIGraphicsEndImageContext()
return UIImage(data: imageData)!
}但质量只是terrible....Heres,这张照片反映了我的心声:

我以为图像的质量会因compressionQuality是什么而变化.?正如您从上面的代码中可以看到的那样,我现在的值是0.5,但是即使我将它更改为1,质量仍然很糟糕吗?
任何想法,
非常感谢。
发布于 2016-04-19 15:38:21
您应该使用UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0)代替。它将对您的设备使用正确的刻度。您使用的版本(没有选项)使用1.0的默认缩放因子。使用...WithOptions()变量并为缩放传递0.0,默认为设备的本机比例尺。请注意,这可能不是您想要的。
其次,可以使用AVMakeRectWithAspectRatioInsideRect()计算目标大小。第三,不需要经过JPEG编码,可以直接返回生成的图像。
综合起来,这大大简化了您的代码:
func resizeImage(image: UIImage) -> UIImage {
let maxSize = CGSize(
width: text.frame.size.width - 10,
height: 600.0
)
let newSize = AVMakeRectWithAspectRatioInsideRect(
image.size,
CGRect(origin: CGPointZero, size: maxSize)
).size
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
image.drawInRect(CGRect(origin: CGPointZero, size: newSize))
let scaled = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaled
}编辑:这是我在项目中使用的UIImage上的一个扩展:
extension UIImage {
/**
Returns an scaled version of self that fits within the provided bounding box.
It retains aspect ratio, and also retains the rendering mode.
- parameter size: The target size, in point
- parameter scale: Optional target scale. If omitted, uses the scale of input image
- returns: A scaled image
*/
func imageFittingSize(size: CGSize, scale: CGFloat? = nil) -> UIImage {
let newSize = AVMakeRectWithAspectRatioInsideRect(self.size, CGRect(origin: CGPointZero, size: size)).size
UIGraphicsBeginImageContextWithOptions(newSize, false, scale ?? self.scale)
self.drawInRect(CGRect(origin: CGPointZero, size: newSize))
let scaled = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return scaled.imageWithRenderingMode(renderingMode)
}
}您可以在这样的图像上调用它:
let maxSize = CGSize(width: text.frame.size.width - 10, height: 600.0)
let scaled = image.imageFittingSize(maxSize)https://stackoverflow.com/questions/36722402
复制相似问题