这是通过一个不大于1的缩放系数来实现,写在UIImage的category中。
+(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize;
先看普通的
+(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{ UIGraphicsBeginImageContext(CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize)); [image drawInRect:CGRectMake(0, 0, image.size.width * scaleSize, image.size.height * scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }
但这样会出现失真问题,图片会变得模糊。应用下边的
+(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize{ CGSize newSize = CGSizeMake(image.size.width * scaleSize, image.size.height * scaleSize); UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0); [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; }