我有臨時變量tmpPixelBuffer
與像素緩衝區數據,它不是nil
,並且當檢測到元數據對象時,我想從該緩衝區創建圖像,以便我可以從該圖像裁剪元數據圖像。如何將CVImageBuffer轉換爲UIImage?
圖片總是nil
,我該怎麼做?
func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) {
tmpPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer)
}
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
let image = CIImage(CVPixelBuffer: tmpPixelBuffer)
let context = CIContext()
let cgiImage = context.createCGImage(image, fromRect: image.extent())
let capturedImage = UIImage(CGImage: cgiImage)
...
}
我也試圖那樣做:
func captureOutput(captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [AnyObject]!, fromConnection connection: AVCaptureConnection!) {
let image = CIImage(CVPixelBuffer: tmpPixelBuffer)
let context = CIContext(options: nil)
let cgiImage = context.createCGImage(image, fromRect: CGRect(x: 0, y: 0, width: Int(CVPixelBufferGetWidth(tmpPixelBuffer)), height: Int(CVPixelBufferGetHeight(tmpPixelBuffer))))
...
}
但在這種情況下的UIImage無法讀取。
我的猜測是,如果輸出格式不是BGRA,那麼你得到的垃圾回來。只是一個猜測。順便說一句,你爲什麼沒有給你一個給你一個非常好的功能(我知道 - 它的ObjC)的Andrea的答案。 – 2017-06-19 20:42:09