如何在iOS中缩小图像,抗锯齿但不柔和?
我尝试过流行的 UIImage+Resize 类别,并且具有不同的插值设置。我尝试过通过 CG 方法和 CIFilters 进行缩放。然而,我永远无法得到缩小后的图像,其焦点看起来既不稍微柔和,也不充满锯齿状伪影。是否有其他解决方案或第三方库可以让我获得非常清晰的图像?
这在 iPhone 上一定是可能的,因为例如“照片”应用程序即使在捏合缩小图像时也会显示清晰的图像。
I have tried the UIImage+Resize category that's popular, and with varying interpolation settings. I have tried scaling via CG methods, and CIFilters. However, I can never get an image downsized that does not either look slightly soft in focus, nor full of jagged artifacts. Is there another solution, or a third party library, which would let me get a very crisp image?
It must be possible on the iPhone, because for instance the Photos app will show a crisp image even when pinching to scale it down.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你说的是CG,但没有具体说明你的方法。
使用绘图或位图上下文:
并确保您的视图及其图层不会再次调整图像大小。我在这方面取得了很好的成果。其他视图可能会影响您的视图或上下文。如果看起来不太好,请单独尝试,以检查是否有某些东西扭曲了您的视图/图像。
如果您要绘制位图,则可以创建具有目标尺寸的位图,然后绘制到该位图。
理想情况下,您将保持纵横比。
另请注意,这可能会占用大量 CPU 资源 - 在 HQ 中重复绘制/缩放会花费大量时间,因此您可能需要创建一个调整大小的副本(使用 CGBitmapContext)。
You said CG, but did not specify your approach.
Using drawing or bitmap context:
and make sure your views and their layers are not resizing the image again. I've had good results with this. It's possible other views are affecting your view or the context. If it does not look good, try it in isolation to sanity check whether or not something is distorting your view/image.
If you are drawing to a bitmap, then you create the bitmap with the target dimensions, then draw to that.
Ideally, you will maintain the aspect ratio.
Also note that this can be quite CPU intensive -- repeatedly drawing/scaling in HQ will cost a lot of time, so you may want to create a resized copy instead (using
CGBitmapContext
).这是我为此编写的例程。有一点柔焦,不过取决于您缩放原始图像的程度,这还不错。我正在缩放程序屏幕截图图像。
Here is the Routine that I wrote to do this. There is a bit of soft focus, though depending on how far you are scaling the original image its not too bad. I'm scaling programatic Screen Shot Images.
CGContextSetInterpolationQuality 就是您正在寻找的。
您应该尝试添加此类别
http://vocaro.com/特雷弗/博客/2009/10/12/resize-a-uiimage-the-right-way/
CGContextSetInterpolationQuality is what you are looking for.
You should try this category additions
http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/
当图像缩小时,应用一些锐化通常是个好主意。
问题是,iOS 上的 Core Image 尚未实现锐化滤镜(
CISharpenLuminance
、CIUnsharpMask
),因此您必须自己推出。或者唠叨苹果,直到他们也在 iOS 上实现这些过滤器。然而,锐化亮度和锐化蒙版是相当先进的滤镜,在之前的项目中我发现即使是简单的 3x3 内核也能产生清晰可见且令人满意的结果。
因此,如果您想在像素级别工作,您可以从图形上下文中获取图像数据,以位掩码方式获取 R、G 和 B 值,并像 1999 年那样对图形进行编码。将会 不过,这有点像重新发明轮子。
也许有一些标准图形库也可以做到这一点(ImageMagick?)
When an image is scaled down, it is often a good idea to apply some sharpening.
The problem is, Core Image on iOS does not (yet) implement the sharpening filters (
CISharpenLuminance
,CIUnsharpMask
), so you would have to roll your own. Or nag Apple till they implement these filters on iOS, too.However, Sharpen luminance and Unsharp mask are fairly advanced filters, and in previous projects I have found that even a simple 3x3 kernel would produce clearly visible and satisfactory results.
Hence, if you feel like working at the pixel level, you could get the image data out of a graphics context, bit mask your way to R, G and B values and code graphics like it is 1999. It will be a bit like re-inventing the wheel, though.
Maybe there are some standard graphics libraries around that can do this, too (ImageMagick?)