问题描述
我正在使用Swift iOS8并尝试将UIImage转换为vImage_Buffer以使用Accelerate.framework执行操作。我正在尝试使用vImageBuffer_initWithCGImage进行转换,这是我尝试的代码:
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
没有编译错误,我尝试运行它我在vImageBuffer_InitWithCGImage行上得到致命错误:在展开可选值时意外发现nil。
我不知道为什么会发生这种情况,因为缓冲区和第3个参数都是backgroundColor都是不安全的并且根据文档应该接受NULL,对于UnsafeMutablePointer来说,它在swift中是nil(如果我错了,请纠正我)。
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).
编辑:
ok我至少理解为什么现在发生这种情况。因为我发送了unwrapped& buffer!。但我无法发送&缓冲区,因为它说vImage_buffer!与vImage_buffer不同。
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".
我尝试将所有类型的组合声明为缓冲区为vImage_buffer?但我无法使其成功。我基本上只需要分配缓冲区参数将其传递给init函数,但我不知道如何...。请帮助
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
推荐答案
在通话之前创建一个虚拟的vImage_Buffer对象:
Just create a dummy vImage_Buffer object before the call:
var buf = vImage_Buffer(data: nil, height: 0, width: 0, rowBytes: 0)
然后致电:
vImageBuffer_InitWithCGImage(&buffer, ... )
并且vImage将正确填写所有结构字段。
and vImage will fill in all the struct fields correctly for you.
这篇关于“致命错误:在展开可选值时意外地发现nil”在Swift ios中使用vImageBuffer_initWithCGImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!