我正在创建一个需要对图像实时应用滤镜的应用程序。将 UIImage
转换为 CIImage
,并应用滤镜都非常快,但是将创建的 CIImage
转换回 CGImageRef
并显示图像需要太长时间(1/5 秒,如果编辑需要实时,则实际上相当长)。
图像大约有 2500 x 2500 像素,这很可能是问题的一部分。
目前,我正在使用:
let image: CIImage //CIImage with applied filters
let eagl = EAGLContext(API: EAGLRenderingAPI.OpenGLES2)
let context = CIContext(EAGLContext: eagl, options: [kCIContextWorkingColorSpace : NSNull()])
//this line takes too long for real-time processing
let cg: CGImage = context.createCGImage(image, fromRect: image.extent)
我已经研究了使用EAGLContext.drawImage()
context.drawImage(image, inRect: destinationRect, fromRect: image.extent)
然而,我找不到任何确切的文档说明如何完成这个过程,或者是否有更快的方法。
是否有更快速的方式将 CIImage
显示到屏幕上(无论是在 UIImageView
中还是直接在 CALayer
上)?我希望避免过度降低图像质量,因为这可能会被用户注意到。
commit()
之后必须立即在MTKView
上调用draw()
,并且还必须将视图的device
设置为创建的设备,并将其framebufferOnly
设置为false
。 - AshCIRenderDestination
和CIContext
上的startTask
方法。 - Flex MonkeyrenderImage()
函数重命名为override func draw()
,在其末尾调用super.draw()
,并在image
的didSet
中将needsDisplay
设置为true
而不是调用旧函数。 - Ash