调整图像大小而不会丢失质量

我正在使用下面的函数来调整我的图像的宽度和高度,但我注意到它毁了图像质量。

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); } 

有没有另一种方式来调整图像大小而不损坏质量? 或者我怎样才能修复我的function,所以它不会毁坏调整后的图像质量?

这是我的Objective-C代码。 这对我有用。

 + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize isAspectRation:(BOOL)aspect { if (!image) { return nil; } //UIGraphicsBeginImageContext(newSize); // In next line, pass 0.0 to use the current device's pixel scaling factor (and thus account for Retina resolution). // Pass 1.0 to force exact pixel size. CGFloat originRatio = image.size.width / image.size.height; CGFloat newRatio = newSize.width / newSize.height; CGSize sz; if (!aspect) { sz = newSize; }else { if (originRatio < newRatio) { sz.height = newSize.height; sz.width = newSize.height * originRatio; }else { sz.width = newSize.width; sz.height = newSize.width / originRatio; } } CGFloat scale = 1.0; // if([[UIScreen mainScreen]respondsToSelector:@selector(scale)]) { // CGFloat tmp = [[UIScreen mainScreen]scale]; // if (tmp > 1.5) { // scale = 2.0; // } // } sz.width /= scale; sz.height /= scale; UIGraphicsBeginImageContextWithOptions(sz, NO, scale); [image drawInRect:CGRectMake(0, 0, sz.width, sz.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 

我在Swift看到了你的代码。 我只是将我的Objective-C转换成Swift而没有testing。 你可以试试看,让我知道。 谢谢!

 struct CommonUtils { static func imageWithImage(image: UIImage, scaleToSize newSize: CGSize, isAspectRation aspect: Bool) -> UIImage{ let originRatio = image.size.width / image.size.height;//CGFloat let newRatio = newSize.width / newSize.height; var sz: CGSize = CGSizeZero if (!aspect) { sz = newSize }else { if (originRatio < newRatio) { sz.height = newSize.height sz.width = newSize.height * originRatio }else { sz.width = newSize.width sz.height = newSize.width / originRatio } } let scale: CGFloat = 1.0 sz.width /= scale sz.height /= scale UIGraphicsBeginImageContextWithOptions(sz, false, scale) image.drawInRect(CGRectMake(0, 0, sz.width, sz.height)) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage } } 
 func image(image:UIImage,imageSize:CGSize)->UIImage { UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0) [image .drawInRect(CGRectMake(0, 3, imageSize.width, imageSize.height))] let newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext() return newImage; }