How to scale down a UIImage and make it crispy / sharp at the same time instead of blurry?(如何缩小 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 并使其同时变得清晰/锐利而不是模糊?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何缩小 UIImage 并使其同时变得清晰/锐利而不是模糊?
基础教程推荐
- 如何从 logcat 中删除旧数据? 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- AdMob 广告未在模拟器中显示 2022-01-01
