I'm working in Swift iOS8 and trying to convert UIImage to vImage_Buffer to perform manipulations using Accelerate.framework. I'm trying to use vImageBuffer_initWithCGImage for conversion, here is the code that I tried:
func initvImageBufferwithUIImage (image:UIImage)-> vImage_Buffer{
var CGImg:CGImageRef = sunset.CGImage
var inProvider:CGDataProviderRef = CGImageGetDataProvider(CGImg)
var inBitmapData:CFDataRef = CGDataProviderCopyData(inProvider)
var buffer:vImage_Buffer!
var format:vImage_CGImageFormat = vImage_CGImageFormat(bitsPerComponent: 8, bitsPerPixel: 32, colorSpace: nil, bitmapInfo: CGBitmapInfo.ByteOrderDefault, version: 0, decode: nil, renderingIntent: kCGRenderingIntentDefault)
vImageBuffer_InitWithCGImage(&buffer!, &format, nil , sunset.CGImage, 0)
return buffer
There is no compilation error and wen I try to run it I get "fatal error: unexpectedly found nil while unwrapping an Optional value" on the line of vImageBuffer_InitWithCGImage. I'm not sure why it's happening since both buffer and 3rd parameter which is backgroundColor are both unsafe and according to documentation are supposed to accept NULL which is nil in swift for UnsafeMutablePointer (correct me if I'm wrong).
EDIT: ok I at least understood why now its happening. Because I send unwrapped &buffer!. But I CANT send &buffer since it says that "vImage_buffer! is not identical to vImage_buffer".
I tried all kind of combinations with declaring buffer as vImage_buffer? but I cant manage to make it work. I basically just need to allocate the buffer parameter to pass it into init function but I dont know how to ... . Please help