How to change BitmapFilter when drawing with CGContext in a CALayer?

Hi all,


I am a new iOS developer who is trying to translate my Android project into iOS.


The app I am building is a drawing app. It shows users a uncolored image and let them to color it.


Now I am facing a problem that when I draw an image onto a CALayer using my customed draw function. To support zoom and drag operation, I update drawRect according to user's touch event, but the image always get blurred because the drawing size is bigger than the actual size.

override func draw(_ layer: CALayer, in ctx: CGContext) {
     ctx.draw(img, in: drawRect)
}

The blurred part has just two colors to highlight users which part is currently selected.


Blur only happends at the boundary of two colors.


I have noticed that changing the layer.magnificationFilter into nearest may help. But it seem that the filter works only if I set image to the layer with layer.contents, overriding draw function cannot benefit from the filter any more.


How can I set a nearest filter for bitmap when drawing with context?


Desperately waiting for tyhe answer...

Replies

Hi all,


I've found the answer, just simply add following code will disable linear filter when drawing a scaled image.

ctx.interpolationQuality = .none