绘制CIImage太慢了

Joj*_*dmo 7 core-graphics ios ciimage swift

我正在创建一个应用程序,需要实时应用过滤器到图像.将UIImagea 转换为a CIImage,并应用滤镜都是非常快速的操作,但将创建的CIImage背面转换为a 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)
Run Code Online (Sandbox Code Playgroud)

我已经考虑过使用了 EAGLContext.drawImage()

context.drawImage(image, inRect: destinationRect, fromRect: image.extent)
Run Code Online (Sandbox Code Playgroud)

然而,我找不到任何关于如何做到这一点的可靠文档,或者它是否会更快

是否有更快的方式来显示CIImage屏幕(在a中UIImageView,或直接在a上CALayer)?我想避免过多地降低图像质量,因为这可能对用户来说是显而易见的.

Sim*_*man 12

可能值得考虑金属和显示与MTKView.

你需要一个可以用它创建的金属设备MTLCreateSystemDefaultDevice().这用于创建命令队列和Core Image上下文.这两个对象都是持久的并且实例化起来非常昂贵,所以理想情况下应该创建一次:

lazy var commandQueue: MTLCommandQueue =
{
    return self.device!.newCommandQueue()
}()

lazy var ciContext: CIContext =
{
    return CIContext(MTLDevice: self.device!)
}()
Run Code Online (Sandbox Code Playgroud)

你还需要一个色彩空间:

let colorSpace = CGColorSpaceCreateDeviceRGB()!
Run Code Online (Sandbox Code Playgroud)

在渲染a时CIImage,您需要创建一个短暂的命令缓冲区:

let commandBuffer = commandQueue.commandBuffer()
Run Code Online (Sandbox Code Playgroud)

你想要把你的CIImage(让它叫它image)呈现给currentDrawable?.texturea MTKView.如果这是绑定的targetTexture,渲染语法是:

    ciContext.render(image,
        toMTLTexture: targetTexture,
        commandBuffer: commandBuffer,
        bounds: image.extent,
        colorSpace: colorSpace)

    commandBuffer.presentDrawable(currentDrawable!)

    commandBuffer.commit()
Run Code Online (Sandbox Code Playgroud)

这里有一个工作版本.

希望有所帮助!

西蒙


Joj*_*dmo 6

我最终使用了这个context.drawImage(image, inRect: destinationRect, fromRect: image.extent)方法.这是我创建的图像视图类

import Foundation
//GLKit must be linked and imported
import GLKit

class CIImageView: GLKView{
    var image: CIImage?
    var ciContext: CIContext?

    //initialize with the frame, and CIImage to be displayed
    //(or nil, if the image will be set using .setRenderImage)
    init(frame: CGRect, image: CIImage?){
        super.init(frame: frame, context: EAGLContext(API: EAGLRenderingAPI.OpenGLES2))

        self.image = image
        //Set the current context to the EAGLContext created in the super.init call
        EAGLContext.setCurrentContext(self.context)
        //create a CIContext from the EAGLContext
        self.ciContext = CIContext(EAGLContext: self.context)
    }

    //for usage in Storyboards
    required init?(coder aDecoder: NSCoder){
        super.init(coder: aDecoder)

        self.context = EAGLContext(API: EAGLRenderingAPI.OpenGLES2)
        EAGLContext.setCurrentContext(self.context)
        self.ciContext = CIContext(EAGLContext: self.context)
    }

    //set the current image to image
    func setRenderImage(image: CIImage){
        self.image = image

        //tell the processor that the view needs to be redrawn using drawRect()
        self.setNeedsDisplay()
    }

    //called automatically when the view is drawn
    override func drawRect(rect: CGRect){
        //unwrap the current CIImage
        if let image = self.image{
            //multiply the frame by the screen's scale (ratio of points : pixels),
            //because the following .drawImage() call uses pixels, not points
            let scale = UIScreen.mainScreen().scale
            let newFrame = CGRectMake(rect.minX, rect.minY, rect.width * scale, rect.height * scale)

            //draw the image
            self.ciContext?.drawImage(
                image,
                inRect: newFrame,
                fromRect: image.extent
             )
        }
    }   
}
Run Code Online (Sandbox Code Playgroud)

然后,简单地使用它

let myFrame: CGRect //frame in self.view where the image should be displayed
let myImage: CIImage //CIImage with applied filters

let imageView: CIImageView = CIImageView(frame: myFrame, image: myImage)
self.view.addSubview(imageView)
Run Code Online (Sandbox Code Playgroud)

在将UIImage转换为CIImage之前将其大小调整为屏幕大小也有帮助.在高质量图像的情况下,它会加速很多事情.确保在实际保存时使用全尺寸图像.

而已!然后,更新视图中的图像

imageView.setRenderImage(newCIImage)
//note that imageView.image = newCIImage won't work because
//the view won't be redrawn
Run Code Online (Sandbox Code Playgroud)

  • 您可以在图像属性上使用 didSet:`var image: CIImage? { didSet { self.setNeedsDisplay() } }` 然后整个 setRenderImage 方法就过时了。 (2认同)