我有这个错误。我想我有两个选择。哪一个最适合我的代码?这些差异意味着什么?
cannot invoke 'RGBtoHSV' with an argument list of type '(Float,Float,Float)'
RGBtoHSV(CGFloat(r), CGFloat(g), CGFloat(b))
RGBtoHSV(CGFloat(), CGFloat(), CGFloat())
此外,如果您看一下屏幕截图,如果您能给我一些关于其他几个错误的指示,那就太好了。我知道我必须匹配类型,但我不知道语法顺序。 http://i.imgur.com/sAckG6h.png
谢谢
func RGBtoHSV(r : CGFloat, g : CGFloat, b : CGFloat) -> (h : CGFloat, s : CGFloat, v : CGFloat) {
var h : CGFloat = 0.0
var s : CGFloat = 0.0
var v : CGFloat = 0.0
let col = UIColor(red: r, green: g, blue: b, alpha: 1.0)
col.getHue(&h, saturation: &s, brightness: &v, alpha: nil)
return (h, s, v)
}
// process the frame of video
func captureOutput(captureOutput:AVCaptureOutput, didOutputSampleBuffer sampleBuffer:CMSampleBuffer, fromConnection connection:AVCaptureConnection) {
// if we're paused don't do anything
if currentState == CurrentState.statePaused {
// reset our frame counter
self.validFrameCounter = 0
return
}
// this is the image buffer
var cvimgRef:CVImageBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)
// Lock the image buffer
CVPixelBufferLockBaseAddress(cvimgRef, 0)
// access the data
var width: size_t = CVPixelBufferGetWidth(cvimgRef)
var height:size_t = CVPixelBufferGetHeight(cvimgRef)
// get the raw image bytes
let buf = UnsafeMutablePointer<UInt8>(CVPixelBufferGetBaseAddress(cvimgRef))
var bprow: size_t = CVPixelBufferGetBytesPerRow(cvimgRef)
var r:Float = 0.0
var g:Float = 0.0
var b:Float = 0.0
for var y = 0; y < height; y++ {
for var x:UInt8 = 0; x < width * 4; x += 4 { // error: '<' cannot be applied to operands of type 'UInt8' and 'Int'
b += buf[x]
g += buf[x + 1]
r += buf[x + 2]
}
buf += bprow(UnsafeMutablePointer(UInt8)) // error: '+=' cannot be applied to operands of type 'UnsafeMutablePointer<UInt8>' and 'size_t'
}
r /= 255 * (width*height)
g /= 255 * (width*height)
b /= 255 * (width*height)
//}
// convert from rgb to hsv colourspace
var h:Float = 0.0
var s:Float = 0.0
var v:Float = 0.0
RGBtoHSV(r, g, b) // error
最佳答案
您有很多类型不匹配错误。
x
的类型不应该是 UInt8
,因为 x
会增加直到宽度值。
for var x:UInt8 = 0; x < width * 4; x += 4 { // error: '<' cannot be applied to operands of type 'UInt8' and 'Int'
所以修复它如下:
for var x = 0; x < width * 4; x += 4 {
要增加指针地址,可以使用advancedBy()
函数。
buf += bprow(UnsafeMutablePointer(UInt8)) // error: '+=' cannot be applied to operands of type 'UnsafeMutablePointer<UInt8>' and 'size_t'
如下:
var pixel = buf.advancedBy(y * bprow)
还有这一行,
RGBtoHSV(r, g, b) // error
不幸的是,在 Swift 中,CGFloat
和 Float
之间没有隐式转换。因此,您应该显式转换为 CGFloat
。
RGBtoHSV(CGFloat(r), g: CGFloat(g), b: CGFloat(b))
整个编辑后的代码在这里:
func RGBtoHSV(r: CGFloat, g: CGFloat, b: CGFloat) -> (h: CGFloat, s: CGFloat, v: CGFloat) {
var h: CGFloat = 0.0
var s: CGFloat = 0.0
var v: CGFloat = 0.0
let col = UIColor(red: r, green: g, blue: b, alpha: 1.0)
col.getHue(&h, saturation: &s, brightness: &v, alpha: nil)
return (h, s, v)
}
// process the frame of video
func captureOutput(captureOutput:AVCaptureOutput, didOutputSampleBuffer sampleBuffer:CMSampleBuffer, fromConnection connection:AVCaptureConnection) {
// if we're paused don't do anything
if currentState == CurrentState.statePaused {
// reset our frame counter
self.validFrameCounter = 0
return
}
// this is the image buffer
var cvimgRef = CMSampleBufferGetImageBuffer(sampleBuffer)
// Lock the image buffer
CVPixelBufferLockBaseAddress(cvimgRef, 0)
// access the data
var width = CVPixelBufferGetWidth(cvimgRef)
var height = CVPixelBufferGetHeight(cvimgRef)
// get the raw image bytes
let buf = UnsafeMutablePointer<UInt8>(CVPixelBufferGetBaseAddress(cvimgRef))
var bprow = CVPixelBufferGetBytesPerRow(cvimgRef)
var r: Float = 0.0
var g: Float = 0.0
var b: Float = 0.0
for var y = 0; y < height; y++ {
var pixel = buf.advancedBy(y * bprow)
for var x = 0; x < width * 4; x += 4 { // error: '<' cannot be applied to operands of type 'UInt8' and 'Int'
b += Float(pixel[x])
g += Float(pixel[x + 1])
r += Float(pixel[x + 2])
}
}
r /= 255 * Float(width * height)
g /= 255 * Float(width * height)
b /= 255 * Float(width * height)
//}
// convert from rgb to hsv colourspace
var h: Float = 0.0
var s: Float = 0.0
var v: Float = 0.0
RGBtoHSV(CGFloat(r), g: CGFloat(g), b: CGFloat(b)) // error
}
关于swift - 无法使用 '_' 类型的参数列表调用 '_' - 我应该使用这两个选项中的哪一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29862614/