LoginSignup
4
5

More than 3 years have passed since last update.

AVCaptureVideoDataOutputのCMSampleBufferからCVPixelBufferにアクセスするときのお作法

Last updated at Posted at 2021-01-15

CVPixelBufferを取得する場合はCVPixelBufferLockBaseAddressでロックを確保してpixelBufferにアクセス、終わったらCVPixelBufferUnlockBaseAddressでアンロックする

Detecting Human Body Poses in an Image より

// Attempt to lock the image buffer to gain access to its memory.
guard CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly) == kCVReturnSuccess
    else {
        return
}

// Create Core Graphics image placeholder.
var image: CGImage?

// Create a Core Graphics bitmap image from the pixel buffer.
VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &image)

// Release the image buffer.
CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)

DispatchQueue.main.sync {
    delegate.videoCapture(self, didCaptureFrame: image)
}

captureOutput(_:didOutput:from:) | Apple Developer Documentation

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5