In iOS, how do you preserve sharp edges when enlarging a bitmap?

大兔子大兔子 提交于 2019-12-18 09:30:44

问题


When you put a UIImage into a UIImageView that is smaller than the view, and the content mode of the view is ScaleToFit, iOS enlarges the bitmap, as expected. What I have never expected is that it blurs the edges of the pixels it has scaled-up. I can see this might be a nice touch if you're looking at photographs, but in many other cases I want to see those nasty, hard, straight edges!

Does anyone know how you can configure a UIImage or UIImageView to enlarge with sharp pixel edges? That is: Let it look pixellated, not blurred.

Thanks!


回答1:


If you want to scale up any image in UIImageView with sharpen edge, use the following property of CALayer.

imageview.layer.magnificationFilter = kCAFilterNearest;

It seems that magnificationFilter affects an interpolation method of contents of UIView. I recommend you to read an explanation of the property in CALayer.h.

/* The filter types to use when rendering the `contents' property of
 * the layer. The minification filter is used when to reduce the size
 * of image data, the magnification filter to increase the size of
 * image data. Currently the allowed values are `nearest' and `linear'.
 * Both properties default to `linear'. */

@property(copy) NSString *minificationFilter, *magnificationFilter;

I hope that my answer is useful for you.



来源:https://stackoverflow.com/questions/22853863/in-ios-how-do-you-preserve-sharp-edges-when-enlarging-a-bitmap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!