2015-09-18 29 views
1

我正在使用AVCaptureVideoDataOutput通过CGDataProviderCopyData得到了迅速的2.0更新this像素数据和我目前得到fatal error: unexpectedly found nil while unwrapping an Optional value在线:let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as! NSData不能在SWIFT 2

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) { 
print("Capture output running") 
let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) 
CVPixelBufferLockBaseAddress(imageBuffer!, 0) 

let baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer!, 0) 
let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!) 
let width = CVPixelBufferGetWidth(imageBuffer!) 
let height = CVPixelBufferGetHeight(imageBuffer!) 
let colorSpace = CGColorSpaceCreateDeviceRGB() 

let bitmapInfo = UInt32(CGBitmapInfo.ByteOrder32Big.rawValue) 
//Original version of above line 
//var bitmapInfo = CGBitmapInfo.fromRaw(CGImageAlphaInfo.PremultipliedFirst.toRaw())! | CGBitmapInfo.ByteOrder32Little 

let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo) 
let imageRef = CGBitmapContextCreateImage(context) 
CVPixelBufferUnlockBaseAddress(imageBuffer!, 0) 

let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as! NSData 

let pixels = UnsafePointer<UInt8>(data.bytes) 

let imageSize : Int = Int(width) * Int(height) * 4 

var newPixelArray = [UInt8](count: imageSize, repeatedValue: 0) 

for index in 0.stride(to: data.length, by: 4) { 
    newPixelArray[index] = 255 - pixels[index] 
    newPixelArray[index + 1] = 255 - pixels[index + 1] 
    newPixelArray[index + 2] = 255 - pixels[index + 2] 
    newPixelArray[index + 3] = pixels[index + 3] 
    print(newPixelArray[index]) 
} 
//remainder of function 
} 
+0

我想我已经得到了'让位= ...'行错了,但我无法弄清楚如何原来显示的转换线下方 – Ian

+0

如果我更换线'let bitmapInfo = CGBitmapInfo(rawValue:CGImageAlphaInfo.PremultipliedFirst.rawValue).rawValue | CGBitmapInfo.ByteOrder32Little.rawValue'行'让数据...'仍然是致命的错误,但没有消息,我只是得到'(lldb)'印好 – Ian

回答

1

固定!注意let data:NSData...线

func captureOutput(captureOutput: AVCaptureOutput!, didOutputSampleBuffer sampleBuffer: CMSampleBuffer!, fromConnection connection: AVCaptureConnection!) { 
    print("Capture output running") 
    let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) 
    CVPixelBufferLockBaseAddress(imageBuffer!, 0) 

    let baseAddress = CVPixelBufferGetBaseAddressOfPlane(imageBuffer!, 0) 
    let bytesPerRow = CVPixelBufferGetBytesPerRow(imageBuffer!) 
    let width = CVPixelBufferGetWidth(imageBuffer!) 
    let height = CVPixelBufferGetHeight(imageBuffer!) 
    let colorSpace = CGColorSpaceCreateDeviceRGB() 

    let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedFirst.rawValue).rawValue | CGBitmapInfo.ByteOrder32Little.rawValue 

    let context = CGBitmapContextCreate(baseAddress, width, height, 8, bytesPerRow, colorSpace, bitmapInfo) 
    let imageRef = CGBitmapContextCreateImage(context) 
    CVPixelBufferUnlockBaseAddress(imageBuffer!, 0) 


    let data:NSData = CGDataProviderCopyData(CGImageGetDataProvider(imageRef))! 

    let pixels = UnsafePointer<UInt8>(data.bytes) 

    let imageSize : Int = Int(width) * Int(height) * 4 

    var newPixelArray = [UInt8](count: imageSize, repeatedValue: 0) 

    for index in 0.stride(to: data.length, by: 4) { 
     newPixelArray[index] = 255 - pixels[index] 
     newPixelArray[index + 1] = 255 - pixels[index + 1] 
     newPixelArray[index + 2] = 255 - pixels[index + 2] 
     newPixelArray[index + 3] = pixels[index + 3] 
     print(newPixelArray[index+1]) 
    } 
} 
+0

好的修复,也解决了我的崩溃。 –