Slide 32
Slide 32 text
Core Animation / Core Graphics
func drawOn(pixelBuffer: CVPixelBuffer, speed: String) -> CVPixelBuffer? {
let colorSpace = CGColorSpace(name: CGColorSpace.sRGB)!
guard let cgContext = CGContext(
data: nil, width: width, height: height,
bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace,
bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue
) else {
return nil
}
let overlayLayer = CALayer()
overlayLayer.render(in: cgContext)
let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
let overlayImage = cgContext.makeImage()!
let overlayCI = CIImage(cgImage: overlayImage)
let compositedImage = overlayCI.composited(over: ciImage)
var outputPixelBuffer: CVPixelBuffer?
CVPixelBufferCreate(
kCFAllocatorDefault, width, height,
CVPixelBufferGetPixelFormatType(pixelBuffer), nil,
&outputPixelBuffer
)
ciContext.render(compositedImage, to: outputBuffer)
return outputBuffer
}