let attrs = [kCVPixelBufferCGImageCompatibilityKey: kCFBooleanTrue, kCVPixelBufferCGBitmapContextCompatibilityKey: kCFBooleanTrue] as CFDictionary
var pixelBuffer : CVPixelBuffer?
let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(image.size.width), Int(image.size.height), kCVPixelFormatType_32BGRA, attrs, &pixelBuffer)
guard (status == kCVReturnSuccess) else {
return nil
}
let context = CIContext()
context.render(CIImage(image: image)!, to: pixelBuffer!)
CVPixelBufferLockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))
let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer!)
let baseAddress = CVPixelBufferGetBaseAddress(pixelBuffer!)
let buffer = baseAddress!.assumingMemoryBound(to: UInt8.self)
print("bytesPerRow ", bytesPerRow)
for cy in 0..<Int(image.size.height){
for cx in 0..<Int(image.size.width){
let index = cx * 4 + cy * bytesPerRow
let a0 = buffer[index]
let r0 = buffer[index+1]
let g0 = buffer[index+2]
let b0 = buffer[index+3]
}
}
CVPixelBufferUnlockBaseAddress(pixelBuffer!, CVPixelBufferLockFlags(rawValue: 0))