Skip to content

Commit

Permalink
Fix creating cgImage from CVImageBuffer failing after using diffe…
Browse files Browse the repository at this point in the history
…rent pixel format
  • Loading branch information
Mattijah committed Nov 16, 2024
1 parent dc7ac4d commit 3f4710e
Showing 1 changed file with 2 additions and 14 deletions.
16 changes: 2 additions & 14 deletions QKMRZScanner/Extensions/CVImageBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,8 @@ import CoreVideo

extension CVImageBuffer {
var cgImage: CGImage? {
CVPixelBufferLockBaseAddress(self, .readOnly)

let baseAddress = CVPixelBufferGetBaseAddress(self)
let bytesPerRow = CVPixelBufferGetBytesPerRow(self)
let (width, height) = (CVPixelBufferGetWidth(self), CVPixelBufferGetHeight(self))
let bitmapInfo = CGBitmapInfo(rawValue: (CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.premultipliedFirst.rawValue))
let context = CGContext.init(data: baseAddress, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: CGColorSpaceCreateDeviceRGB(), bitmapInfo: bitmapInfo.rawValue)

guard let cgImage = context?.makeImage() else {
return nil
}

CVPixelBufferUnlockBaseAddress(self, .readOnly)

let ciImage = CIImage(cvPixelBuffer: self)
let cgImage = CIContext.shared.createCGImage(ciImage, from: ciImage.extent)
return cgImage
}
}

0 comments on commit 3f4710e

Please sign in to comment.