swift - 无法使用 '_' 类型的参数列表调用 '_' - 我应该使用这两个选项中的哪一个?

标签 swift

我有这个错误。我想我有两个选择。哪一个最适合我的代码?这些差异意味着什么?

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 中,CGFloatFloat 之间没有隐式转换。因此,您应该显式转换为 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/

相关文章:

ios - swift:如何更新 NSUserDefault?

ios - UIAlertViewcontroller 在转到 viewcontroller 之前没有被弹出

swift - 你能严格泛型类型或给一个参数多于一种类型吗?

swift - 授予 OSX 沙盒查找器同步扩展持久写入访问权限

objective-c - 如何在 xcode 中预览基于自定义代码的 View ?

ios - 计算内存中图像的大小(以字节为单位)

swift - deinit 末尾的 Defer 语句会产生警告

swift - 在 SQLite.swift 中通过正确绑定(bind)从任意 SQL 语句获取结果

ios - 在 Objective-C 中使用 #define 并在 swift 类中访问

swift - 将模型与裸 RxSwift 和 BehaviourSubject 同步