渲染以MTKView为中心的小型CIImage [英] Rendering small CIImage centered in MTKView

查看:289
本文介绍了渲染以MTKView为中心的小型CIImage的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将CIImage渲染为MTKView,并且图像小于可绘制对象.

I'm rendering a CIImage to MTKView and the image is smaller than the drawable.

let centered = image.transformed(by: CGAffineTransform(translationX: (view.drawableSize.width - image.extent.width) / 2, y: (view.drawableSize.height - image.extent.height) / 2))
context.render(centered, to: drawable.texture, commandBuffer: buffer, bounds: centered.extent, colorSpace: CGColorSpaceCreateDeviceRGB())

我希望上面的代码可以在视图的中心渲染图像,但是图像位于原点.

I'd expect the code above to render the image in the center of the view, but the image is positioned at origin instead.

以下是说明问题的存储库: https://github. com/truemetal/centered-render-of-ciimage-to-mtkview

Here's the repo illustrating the problem: https://github.com/truemetal/centered-render-of-ciimage-to-mtkview

在指责MetalCoreImage之前,我想确保自己没有做错什么.

Before blaming Metal or CoreImage I'd like to make sure I'm not doing something wrong.

我非常感谢指向文档的链接,该链接指出我无法执行此类操作.

I'd appreciate a link to the documentation that says I can't do something like that.

我可以通过将图像合成到另一个恰好是可绘制对象大小的图像上来解决此问题,但是我仍然对为什么上面的代码无法正常工作感兴趣.

I can workaround this by compositing the image over another one that would be exactly the size of the drawable like so, but I'm still interested in why exactly the code above does not work.

let centered = image.transformed(by: CGAffineTransform(translationX: (view.drawableSize.width - image.extent.width) / 2, y: (view.drawableSize.height - image.extent.height) / 2))
let background = CIImage(color: .white).cropped(to: CGRect(origin: .zero, size: view.drawableSize))
let preparedImage = centered.composited(over: background)
self.context.render(preparedImage, to: drawable.texture, commandBuffer: buffer, bounds: preparedImage.extent, colorSpace: CGColorSpaceCreateDeviceRGB())

推荐答案

这很好奇.如果您使用新的" CIRenderDestination API而不是context.render(…),则它实际上有效:

This is most curious. If you use the "new" CIRenderDestination API instead of context.render(…) it actually works:

let destination = CIRenderDestination(width: Int(view.drawableSize.width),
                                      height: Int(view.drawableSize.height),
                                      pixelFormat: view.colorPixelFormat,
                                      commandBuffer: buffer,
                                      mtlTextureProvider: { () -> MTLTexture in
                                          return drawable.texture
                                      })
try! self.context.startTask(toRender: centered, to: destination)

我不知道为什么,但是context.render(…)似乎不尊重图像或给定范围的翻译.也许其他人知道更多...

I don't know why, but context.render(…) doesn't seem to respect the translation of the image or the given bounds. Maybe someone else knows more...

这篇关于渲染以MTKView为中心的小型CIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆