我正在创建一个需要对图片实时应用过滤器的应用。将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
)?我想避免过多地降低图像质量,因为用户可能会注意到这一点。
答案 0 :(得分:10)
值得考虑使用Metal并使用MTKView
进行显示。
您需要一个可以使用MTLCreateSystemDefaultDevice()
创建的Metal设备。这用于创建命令队列和Core Image上下文。这两个对象都是持久的并且实例化起来非常昂贵,因此理想情况下应该创建一次:
lazy var commandQueue: MTLCommandQueue =
{
return self.device!.newCommandQueue()
}()
lazy var ciContext: CIContext =
{
return CIContext(MTLDevice: self.device!)
}()
您还需要一个色彩空间:
let colorSpace = CGColorSpaceCreateDeviceRGB()!
在渲染CIImage
时,您需要创建一个短命命令缓冲区:
let commandBuffer = commandQueue.commandBuffer()
您希望将CIImage
(让我们称之为image
)呈现给currentDrawable?.texture
的{{1}}。如果它绑定到MTKView
,则呈现语法为:
targetTexture
我有一个正常工作的版本here。
希望有所帮助!
西蒙
答案 1 :(得分:5)
我最终使用了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
)
}
}
}
然后,要使用它,只需
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)
在将UIImage转换为CIImage之前将其大小调整为屏幕大小也有帮助。在高质量图像的情况下,它会加速很多事情。确保在实际保存时使用全尺寸图像。
多数民众赞成!然后,更新视图中的图像
imageView.setRenderImage(newCIImage)
//note that imageView.image = newCIImage won't work because
//the view won't be redrawn
答案 2 :(得分:2)
您可以使用GlkView并使用context.drawImage():
进行渲染let glView = GLKView(frame: superview.bounds, context: EAGLContext(API: .OpenGLES2))
let context = CIContext(EAGLContext: glView.context)
处理后渲染图像:
glView.bindDrawable()
context.drawImage(image, inRect: destinationRect, fromRect: image.extent)
glView.display()
答案 3 :(得分:0)
这是一个非常大的形象,所以它绝对是其中的一部分。我建议您查看GPUImage进行单张图片过滤。您可以完全跳过使用CoreImage。
let inputImage:UIImage = //... some image
let stillImageSource = GPUImagePicture(image: inputImage)
let filter = GPUImageSepiaFilter()
stillImageSource.addTarget(filter)
filter.useNextFrameForImageCapture()
stillImageSource.processImage()