ich verschiedene Bilder mit diesem func bin Ändern der Größe:Qualität der UIImage
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)!
}
Aber die Qualität ist einfach schrecklich .... Heres ein Bild von dem, was ich unten:
Ich dachte, dass die Qualität des Bildes davon abwich, was die Komprimierungsqualität war ...? Ich habe es jetzt bei 0,5, wie Sie aus meinem Code oben sehen können, aber selbst wenn ich es auf 1 ändere, ist die Qualität immer noch schrecklich?
Irgendwelche Ideen,
Vielen Dank.
0,5 Kompressionsverhältnis in der Originalgröße passt scheint ein Unschärfe-Effekt zu sein? – zcui93
@ zcui93 nicht sicher, ich verstehe, was du meinst –
Ihr Code scheint in Ordnung zu sein. Können Sie der Frage originale und modifizierte Versionen desselben Bildes hinzufügen? – Avt