如何缩小 UIImage 并使其同时变得清晰/锐利而不是模糊? [英] How to scale down a UIImage and make it crispy / sharp at the same time instead of blurry?

查看:18
本文介绍了如何缩小 UIImage 并使其同时变得清晰/锐利而不是模糊?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要缩小图像,但要以锐利的方式.例如,在 Photoshop 中有图像尺寸缩小选项Bicubic Smoother"(模糊)和Bicubic Sharper".

I need to scale down an image, but in a sharp way. In Photoshop for example there are the image size reduction options "Bicubic Smoother" (blurry) and "Bicubic Sharper".

此图像缩小算法是否在某处开源或记录在案,或者 SDK 是否提供执行此操作的方法?

Is this image downscaling algorithm open sourced or documented somewhere or does the SDK offer methods to do this?

推荐答案

仅仅使用 imageWithCGImage 是不够的.它会缩放,但无论放大还是缩小,结果都会变得模糊和次优.

Merely using imageWithCGImage is not sufficient. It will scale, but the result will be blurry and suboptimal whether scaling up or down.

如果你想获得正确的别名并摆脱锯齿",你需要这样的东西:http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/.

If you want to get the aliasing right and get rid of the "jaggies" you need something like this: http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/.

我的工作测试代码看起来像这样,这是 Trevor 的解决方案,只需稍加调整即可使用我的透明 PNG:

My working test code looks something like this, which is Trevor's solution with one small adjustment to work with my transparent PNGs:

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

这篇关于如何缩小 UIImage 并使其同时变得清晰/锐利而不是模糊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆