Swift-技巧(八)CVPixelBuffer To CGImage(Swift skill (VIII) cvpixelbuffer to cgimage)

摘要
Swift 中图像的表现形式不只是 Image,还有更加底层的方式,比如 像素缓存形式,那么像素缓存转换为可以在应用中展示的 CGImage,就要知道有哪些处理了。

摘要

Swift 中图像的表现形式不只是 Image,还有更加底层的方式,比如 像素缓存形式,那么像素缓存转换为可以在应用中展示的 CGImage,就要知道有哪些处理了。

CVPixelBuffer

CGImage 苹果官方解释是一张 bitmap 图像或者图像 mask。它是 UIImage 类中的一个属性,并可以通过 的初始化函数称为一个 Image 对象。

UIImage

CVPixelBuffer 是核心缓存像素对象的引用,这里存储着一张图像。

在有些应用场景中,需要把 转换为 ,以便用来展示。

CVPixelBuffer
CGImage

CVPixelBuffer To CGImage

CVPixelBuffer
CGImage

转换获得 对象,可以使用 函数处理,但不是所有的 对象都可以支持转换。

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

在使用以上函数前,要导入框架声明 ,毕竟不是 框架的函数。

VideoToolbox
UIKit
import VideoToolbox

除此之外,还可以使用 Core Image 作为桥梁,间接达转换效果。这里要用到 和 的对象处理。

CIImage(cvPixelBuffer:)
CIContext
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)
}

若没有 对象传入的话,就可以直接自己创建一个 对象,这个只是作为绘图的画布,没有非常高深的地方。

context
CIContext

使用这个函数也是需要导入框架 。

CoreImage

最后一种方式,也能达到转换的效果,就是使用 方法,它可以使用 对象存储创建一个 bitmap 的 。像素格式可以支持 32ARGB,也可以更改 和 属性的变量,达到改变像素格式。这种方式更加灵活,同时也相对来说比较复杂,如果有精力,可以适当了解一下。

CGContext
CVPixelBuffer
CGContext
bitmapInfo
space
// 使用之前记得导入框架
import CoreGraphics

public static func create(pixelBuffer: CVPixelBuffer) -> 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
  }
}

代码中创建 对象的初始化函数中的属性,都是一张图像中必须要有的信息,若对这感兴趣,给我留言,我会详细说一下图像。

context

题外话

时间仓促,说的东西可能不全面,在你查看的过程中遇到什么问题,评论区给我留言,我会尽快回复

————————

abstract
The image representation in swift is not only image, but also a more underlying way, such as pixel cache. If the pixel cache is converted into cgimage that can be displayed in the application, you need to know what processing is involved.

abstract

The image representation in swift is not only image, but also a more underlying way, such as pixel cache. If the pixel cache is converted into cgimage that can be displayed in the application, you need to know what processing is involved.

CVPixelBuffer

Cgimage Apple’s official explanation is a bitmap image or image mask. It is an attribute in the uiimage class and can be called an image object through its initialization function.

UIImage

Cvpixelbuffer is the reference of the core cache pixel object, where an image is stored.

In some application scenarios, you need to convert to for presentation.

CVPixelBuffer
CGImage

CVPixelBuffer To CGImage

CVPixelBuffer
CGImage

Objects obtained by conversion can be processed by functions, but not all objects can support conversion.

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

Before using the above functions, you should import the framework declaration. After all, it is not a function of the framework.

VideoToolbox
UIKit
import VideoToolbox

In addition, you can also use < strong > core image < / strong > as a bridge to indirectly achieve the conversion effect. The object processing of and is used here.

CIImage(cvPixelBuffer:)
CIContext
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)
}

If no object is passed in, you can directly create an object yourself. This is just a canvas for drawing, and there is no very deep place.

context
CIContext

You also need to import the framework to use this function.

CoreImage

The last way to achieve the effect of conversion is to use the method, which can create a bitmap using object storage. Pixel format can support < strong > 32argb < / strong >, and can also change and attribute variables to change pixel format. This method is more flexible and relatively complex. If you have energy, you can properly understand it.

CGContext
CVPixelBuffer
CGContext
bitmapInfo
space
// 使用之前记得导入框架
import CoreGraphics

public static func create(pixelBuffer: CVPixelBuffer) -> 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
  }
}

The properties in the initialization function of the object created in the code are the necessary information in an image. If you are interested in this, leave me a message and I will talk about the image in detail.

context

Digression

In a hurry, what you said may not be comprehensive. If you encounter any problems in the process of viewing, leave me a message in the comment area and I will reply as soon as possible