2016-11-12 51 views
2

我試圖將庫從Swift 2轉換爲Swift 3版本時遇到vImageHistogramCalculation_ARGB8888方法的問題。問題是,該方法接受「直方圖」的說法只是作爲在Swift 3中繪製CGImage的直方圖

UnsafeMutablePointer<UnsafeMutablePointer<T>?> 

但斯威夫特3建設

let histogram = UnsafeMutablePointer<UnsafeMutablePointer<vImagePixelCount>>(mutating: rgba) 

返回展開值,所以我不能投它正確打字。

編譯器錯誤是:

: Cannot invoke initializer for type 'UnsafeMutablePointer?>' with an argument list of type '(mutating: [UnsafeMutablePointer])'

你有沒有一些想法?我試圖添加「?」以直方圖不變,但後來我收到錯誤:

'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily view memory as another layout-compatible type.

有一些編譯器的建議,但我不知道如何使用它。

回答

4

演員到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 
+0

所以,RGBA我需要調用vImageHistogramCalculation_ARGB8888四次吧?對於每個價值?無論如何..不工作:/不能像你的例子中構建直方圖常量。你試過了嗎? –

+0

已更新的答案。 – shallowThought

+0

有道理謝謝你:) –