【发布时间】:2019-04-02 04:03:53
【问题描述】:
我正在使用一个相当标准的MTKView 子类,它显示一个CIImage,以便使用CIFilters 快速更新。 draw() 函数的代码如下。
我遇到的问题是,只有图像覆盖的视图部分,在本例中为下面代码中的scaledImage,实际上是重绘的。这意味着如果新图像比以前的图像小,我仍然可以看到旧图像的一部分从它后面伸出。
有没有办法在包含的图像大小发生变化时清除drawable的纹理内容,以免发生这种情况?
override func draw() {
guard let image = image,
let targetTexture = currentDrawable?.texture,
let commandBuffer = commandQueue.makeCommandBuffer()
else { return }
let bounds = CGRect(origin: CGPoint.zero, size: drawableSize)
let scaleX = drawableSize.width / image.extent.width
let scaleY = drawableSize.height / image.extent.height
let scale = min(scaleX, scaleY)
let width = image.extent.width * scale
let height = image.extent.height * scale
let originX = (bounds.width - width) / 2
let originY = (bounds.height - height) / 2
let scaledImage = image
.transformed(by: CGAffineTransform(scaleX: scale, y: scale))
.transformed(by: CGAffineTransform(translationX: originX, y: originY))
ciContext.render(
scaledImage,
to: targetTexture,
commandBuffer: commandBuffer,
bounds: bounds,
colorSpace: colorSpace
)
guard let drawable = currentDrawable else { return }
commandBuffer.present(drawable)
commandBuffer.commit()
super.draw()
}
【问题讨论】:
-
您不需要使用
MTKView来对图像应用过滤器。您可以在UIImageView的层上设置过滤器。 -
来自 CALayer 上的 Apple 文档:
This property is not supported on layers in iOS. -
哦,对了。对不起。
-
没有问题,无论如何感谢您的输入:)