我需要缩小图像的比例,但要以一种清晰的方式.例如,在Photoshop中有图像大小缩小选项"双三次平滑"(模糊)和"双三次锐化".
这个图像缩小算法是开源的还是有文档记录的,或者SDK提供了这样做的方法吗?
我需要缩小图像的比例,但要以一种清晰的方式.例如,在Photoshop中有图像大小缩小选项"双三次平滑"(模糊)和"双三次锐化".
这个图像缩小算法是开源的还是有文档记录的,或者SDK提供了这样做的方法吗?
仅仅使用imageWithCGImage
是不够的.它会进行zoom ,但无论是放大还是缩小,结果都会模糊且不理想.
如果你想获得正确的别名,摆脱"锯齿",你需要这样的东西:http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/.
我的工作测试代码如下所示,这是Trevor的解决方案,只做了一个小调整就可以处理我的透明PNG:
- (UIImage *)resizeImage:(UIImage*)image newSize:(CGSize)newSize {
CGRect newRect = CGRectIntegral(CGRectMake(0, 0, newSize.width, newSize.height));
CGImageRef imageRef = image.CGImage;
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
// Set the quality level to use when rescaling
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, newSize.height);
CGContextConcatCTM(context, flipVertical);
// Draw into the context; this scales the image
CGContextDrawImage(context, newRect, imageRef);
// Get the resized image from the context and a UIImage
CGImageRef newImageRef = CGBitmapContextCreateImage(context);
UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
CGImageRelease(newImageRef);
UIGraphicsEndImageContext();
return newImage;
}