我正在使用下面的函数调整图像的宽度和高度,但是我注意到它会破坏图像质量。
class func imageWithSize(image: UIImage,size: CGSize)->UIImage{
if UIScreen.mainScreen().respondsToSelector("scale"){
UIGraphicsBeginImageContextWithOptions(size,false,UIScreen.mainScreen().scale);
}
else
{
UIGraphicsBeginImageContext(size);
}
image.drawInRect(CGRectMake(0, 0, size.width, size.height));
var newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
class func resizeImageWithAspect(image: UIImage,scaledToMaxWidth width:CGFloat,maxHeight height :CGFloat)->UIImage
{
let scaleFactor = width / height;
let newSize = CGSizeMake(width, height);
return imageWithSize(image, size: newSize);
}
有没有其他方法可以调整图像大小而不会破坏质量?或者如何修复我下面的功能,使其在调整大小后不会破坏图像质量?
UIGraphicsBeginImageContextWithOptions
函数是在 iOS 4 中添加的。在现今这个时代检查是否应该使用它已经没有意义了。 - rmaddy