我有一个像素艺术游戏应用程序,该应用程序使用Uikit进行菜单和SpriteKit用于游戏场景。由于抗氧化剂而导致像素变得模糊。
使用精灵,我可以使用...
node.texture?.filteringMode = .nearest
但是在Uikit中,我找不到可以关闭UiimageView中的抗偏置的方法。
我看到了这个 post ,但没有例子,答案没有接受。不确定如何使用 cgcontextsetshouldantialias
将其关闭,或者在哪里调用。
基于示例,我找到了在这里,尝试使用此子类,但似乎没有有所作为;根据我的断点,该方法从未被调用:
class NonAliasingView: UIImageView {
override func draw(_ rect: CGRect) {
guard let ctx = UIGraphicsGetCurrentContext() else { return }
// fill background with black color
ctx.addRect(bounds)
ctx.setFillColor(UIColor.black.cgColor)
ctx.fillPath()
if let img = image {
let pixelSize = CGSize(width: img.size.width * layer.contentsScale, height: img.size.height * layer.contentsScale)
UIGraphicsBeginImageContextWithOptions(pixelSize, true, 1)
guard let imgCtx = UIGraphicsGetCurrentContext() else { return }
imgCtx.setShouldAntialias(false)
img.draw(in: CGRect(x: 0, y: 0, width: pixelSize.width, height: pixelSize.height))
guard let cgImg = imgCtx.makeImage() else { return }
ctx.scaleBy(x: 1, y: -1)
ctx.translateBy(x: 0, y: -bounds.height)
ctx.draw(cgImg, in: CGRect(x: (bounds.width - img.size.width) / 2, y: (bounds.height - img.size.height) / 2, width: img.size.width, height: img.size.height))
}
}
}
以下是我尝试实现子类的代码(ModeImage是uiimageView的IBOUTLET):
// modeImage.image = gameMode.displayImage
let modeImg = NonAliasingView()
modeImg.image = gameMode.displayImage
modeImage = modeImg
如果我尝试在视图中使用 uigraphicsgetCurrentContext 控制器是零,永远不会通过守卫声明。
我已经确认 view.layer.allowsedgeantialiasing
默认为false。我根本不需要抗缩影,因此,如果有一种方法可以关闭宽阔的应用程序,或者在整个视图控制器中,我很乐意使用它。
如何在Uikit中使用UIImageView?
update
添加 imgctx.setshouldantialias(false)在方法中,如何禁用抗氧化。
I've got a pixel art game app that uses UIKit for its menus and SpriteKit for the gameplay scene. The pixels are getting blurred due to anti-aliasing.
With the sprites I can turn off the anti-aliasing using...
node.texture?.filteringMode = .nearest
but in UIKit I can't find a way to turn off the anti-aliasing in the UIImageView's.
I saw this post but there's no example and the answer wasn't accepted. Not sure how to turn it off using CGContextSetShouldAntialias
, or where to call it.
Based on the example I found here, tried using this subclass but it didn't seem to make a difference; according to my breakpoints the method is never called:
class NonAliasingView: UIImageView {
override func draw(_ rect: CGRect) {
guard let ctx = UIGraphicsGetCurrentContext() else { return }
// fill background with black color
ctx.addRect(bounds)
ctx.setFillColor(UIColor.black.cgColor)
ctx.fillPath()
if let img = image {
let pixelSize = CGSize(width: img.size.width * layer.contentsScale, height: img.size.height * layer.contentsScale)
UIGraphicsBeginImageContextWithOptions(pixelSize, true, 1)
guard let imgCtx = UIGraphicsGetCurrentContext() else { return }
imgCtx.setShouldAntialias(false)
img.draw(in: CGRect(x: 0, y: 0, width: pixelSize.width, height: pixelSize.height))
guard let cgImg = imgCtx.makeImage() else { return }
ctx.scaleBy(x: 1, y: -1)
ctx.translateBy(x: 0, y: -bounds.height)
ctx.draw(cgImg, in: CGRect(x: (bounds.width - img.size.width) / 2, y: (bounds.height - img.size.height) / 2, width: img.size.width, height: img.size.height))
}
}
}
Here's the code from my view controller where I tried to implement the subclass (modeImage is an IBOutlet to a UIImageView):
// modeImage.image = gameMode.displayImage
let modeImg = NonAliasingView()
modeImg.image = gameMode.displayImage
modeImage = modeImg
If I try to use UIGraphicsGetCurrentContext
in the view controller it is nil and never passes the guard statement.
I've confirmed view.layer.allowsEdgeAntialiasing
defaults to false. I don't need anti-aliasing at all, so if there's a way to turn off anti-aliasing app wide, or in the whole view controller, I'd be happy to use it.
How do you disable anti-aliasing with a UIImageView in UIKit?
UPDATE
Added imgCtx.setShouldAntialias(false)
to method but still not working.
发布评论
评论(1)
要删除图像视图上的所有抗质量并只需使用最近的邻居过滤,请设置放大式窗口 和
minificationfilter href =“ https://developer.apple.com/documentation/quartzcore/calayercontentsfilter/1410831-Nearest” rel =“ nofollow noreferrer”>
calayerContentsFilter.nearestfilter.nearest。
To remove all antialiasing on your image view and just use nearest-neighbor filtering, set the
magnificationFilter
andminificationFilter
of the image view's layer toCALayerContentsFilter.nearest
, as in: