Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

Abstract

The representation form of images in Swift is not only Image, but also more low-level way, such as CVPixelBuffer pixel cache form. Then the pixel cache is converted into CGImage that can be displayed in the application, you need to know what processing.

CGImage is a bitmap image or image mask. It is a property in the UIImage class and can be called an Image object by UIImage’s initialization function.

CVPixelBuffer is a reference to the core cache pixel object where an image is stored.

In some application scenarios, CVPixelBuffer needs to be converted to CGImage for presentation.

CVPixelBuffer To CGImage

CVPixelBuffer is converted to obtain CGimage objects. Can use VTCreateCGImageFromCVPixelBuffer (_ pixelBuffer: options: imageOut:) – > OSStatus function processing, However, not all CVPixelBuffer objects support conversion.

public static func create(pixelBuffer: CVPixelBuffer) -> CGImage? {
  var cgImage: CGImage?
  VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &cgImage)
  return cgImage
}
Copy the code

Before using the above functions, import the framework declaration VideoToolbox, which is not a UIKit framework function after all.

import VideoToolbox
Copy the code

In addition, you can use Core Image as a bridge to reach the transformation effect indirectly. Here we use CIImage(cvPixelBuffer:) and CIContext object processing.

public static func create(pixelBuffer: CVPixelBuffer, context: CIContext) -> CGImage? {
  let ciImage = CIImage(cvPixelBuffer: pixelBuffer)
  let rect = CGRect(x: 0, y: 0, width: CVPixelBufferGetWidth(pixelBuffer),
                                height: CVPixelBufferGetHeight(pixelBuffer))
  return context.createCGImage(ciImage, from: rect)
}
Copy the code

If no context object is passed in, you can create a CIContext object yourself, which is just a canvas for drawing, not very deep.

Using this function also requires importing the framework CoreImage.

The final way to achieve this transformation is to use the CGContext method, which creates a BITMap CGContext using the CVPixelBuffer object store. Pixel format can support 32ARGB, and you can change the variables of bitmapInfo and space properties to change the pixel format. This method is more flexible, but also relatively complex, if you have the energy, can be appropriate to understand.

Import CoreGraphics public static func create(pixelBuffer: cvelBuffer) -> CGImage? { guard kCVReturnSuccess == CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly) else { return nil } defer { CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly) } if let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer), width: CVPixelBufferGetWidth(pixelBuffer), height: CVPixelBufferGetHeight(pixelBuffer), bitsPerComponent: 8, bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer), space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: CGImageAlphaInfo.noneSkipFirst.rawValue), let cgImage = context.makeImage() { return cgImage } else { return nil } }Copy the code

The properties in the initializer that creates the context object in your code, are all information that you have to have in an image, so if you’re interested in that, leave me a comment, and I’ll talk about the image in more detail.

digression

Time is short and what you said may not be comprehensive. If you have any problems during your review, please leave a message in the comment section and I will reply as soon as possible