关于CIHistogramDisplayFilter的Objective-C到Swift转换问题

时间:2016-05-02 16:41:39

标签: objective-c xcode swift histogram ciimage

我有这段代码片段在Objective-C中显示直方图,效果很好。但是,我很难将其转换为Swift。

目标-C

    var pixelBuffer: CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)!
    var attachments: CFDictionaryRef = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate)!
    var ciImage: CIImage = CIImage(cVPixelBuffer: pixelBuffer, options: (attachments as! [NSObject : AnyObject]))

在将其转换为Swift时,我开始收到这些错误:

Argument labels '(cVPixelBuffer:, options:)' do not match any available overloads

最后一行给出了错误:

ServerBullet b = new ServerBullet(ownerId, rot);
b.x = this.x + (float) (Math.cos(rotRad) * 34);
b.y = this.y + (float) (Math.sin(rotRad) * 34);
bullets.add(b);

1 个答案:

答案 0 :(得分:1)

//Show Histogram -- Swift version
    var pixelBuffer: CVPixelBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer)!
    var attachments: CFDictionaryRef = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate)!
    var ciImage: CIImage = CIImage(CVPixelBuffer: pixelBuffer, options: attachments as? [String : AnyObject])

    var count = 256
    count = count <= 256 ? count : 256
    count = count >= 1 ? count : 1

    let params = [kCIInputImageKey: ciImage,
        kCIInputExtentKey:CIVector.init(CGRect: ciImage.extent),
        "inputCount":256, "inputScale":200]

    let filter:CIFilter! = CIFilter(name: "CIAreaHistogram" , withInputParameters: params)


    let outImage: CIImage = (filter?.outputImage)!
    //---------------------------------------------

    let context:CIContext = CIContext(options: nil)

    let params2 = [kCIInputImageKey: outImage]


    let filter2 = CIFilter(name: "CIHistogramDisplayFilter",withInputParameters: params2)

    let outputImage:CIImage = filter2!.outputImage!
    let outExtent:CGRect = outputImage.extent

    let cgImage:CGImageRef = context.createCGImage(outputImage, fromRect: outExtent)

    let outImage2:UIImage = UIImage(CGImage: cgImage)

    // resize

    let resized:UIImage = self.resizeImage(outImage2,withQuality:CGInterpolation.None,rate:2.5)

    //Remove the default grey background

    resized = self.removeColorFromImage(resized, grayLevel:137)


    dispatch_async(dispatch_get_main_queue()) {
        self.histogramView.image = resized
    }