在swift 2中无法使用AVCaptureVideoDataOutput通过CGDataProviderCopyData获取像素数据
我正在更新这个 swift 2.0,我目前得到fatal error: unexpectedly found nil while unwrapping an Optional value
上线: let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as! NSData
let data = CGDataProviderCopyData(CGImageGetDataProvider(imageRef)) as! 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 = 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 }
固定! 注意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]) } }
- 当NSOperationQueue中的所有其他操作完成时,执行NSOperation,无论它们是否成功完成
- 使用performBackgroundTask更新NSFetchedResultsController
- iOS外围设备服务发现后仍为空
- 隐藏IOS快速应用程序中的标签栏
- iOS7和iOS8:如何检测用户对推送通知的请求是“否”
- 命令/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang失败,退出
- 引用'CMTime'是不明确的
- 以编程方式显示或隐藏UIToolbar中的UIBarButtonItem
- 我怎样才能添加相同的背景图像到我的IOS应用程序中的所有视图使用SWIFT?