Swift 3中CGImage的绘制直方图 [英] Drawing histogram of CGImage in Swift 3
问题描述
尝试将库从Swift 2版本转换为Swift 3版本时, vImageHistogramCalculation_ARGB8888
方法有问题。问题在于该方法仅接受
I have a problem with vImageHistogramCalculation_ARGB8888
method while trying to convert library from Swift 2 to Swift 3 version. The problem is that the method accepts "histogram" argument only as
UnsafeMutablePointer<UnsafeMutablePointer<T>?>
但是Swift 3构造
but Swift 3 construction
let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>>(mutating: rgba)
返回未包装的值,因此无法将其强制转换为正确的类型。
return unwrapped value, so I can't cast it to properly type.
编译器错误是:
:无法为类型$ b调用初始化程序$ b'UnsafeMutablePointer?>'和
类型为'(muting:
[UnsafeMutablePointer])'的参数列表'
: Cannot invoke initializer for type 'UnsafeMutablePointer?>' with an argument list of type '(mutating: [UnsafeMutablePointer])'
您有一些想法吗?我试图添加?到直方图常量,但随后收到错误:
Have you some ideas? I tried to add "?" to histogram constant but then I receive error:
'init'不可用:对$使用$ withMemoryRebound(to:capacity:_) b $ b暂时将内存视为另一种与布局兼容的类型。
'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.
有一些编译器建议,但我不知道如何使用它。
there is some compiler suggestion but I have no idea how can use it.
推荐答案
是否已转换为 UnsafeMutablePointer< vImagePixelCount>?:
let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: alpha) as UnsafeMutablePointer<vImagePixelCount>?
完成,编译示例:
var inBuffer = vImage_Buffer() // set your buffer here
let alpha = [UInt](repeating: 0, count: 256)
let red = [UInt](repeating: 0, count: 256)
let green = [UInt](repeating: 0, count: 256)
let blue = [UInt](repeating: 0, count: 256)
let alphaPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: alpha) as UnsafeMutablePointer<vImagePixelCount>?
let redPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: red) as UnsafeMutablePointer<vImagePixelCount>?
let greenPtr = UnsafeMutablePointer<vImagePixelCount>(mutating: green) as UnsafeMutablePointer<vImagePixelCount>?
let bluePtr = UnsafeMutablePointer<vImagePixelCount>(mutating: blue) as UnsafeMutablePointer<vImagePixelCount>?
let rgba = [redPtr, greenPtr, bluePtr, alphaPtr]
let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>?>(mutating: rgba)
let error = vImageHistogramCalculation_ARGB8888(&inBuffer, histogram, UInt32(kvImageNoFlags))
// alpha, red, green, blue are set now
这篇关于Swift 3中CGImage的绘制直方图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!