CIHistogramDisplayFilter上的Objective-C到Swift转换问题



我有这个代码片段来显示Objective-C中的直方图,它工作得很好。然而,我很难把它转换成Swift。

Objective-C

//Show Histogram
        CVPixelBufferRef pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
        CFDictionaryRef attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, kCMAttachmentMode_ShouldPropagate);
        CIImage *ciImage = [[CIImage alloc] initWithCVPixelBuffer:pixelBuffer options:(__bridge NSDictionary *)attachments];

        NSUInteger count = 256;
        count = count <= 256 ? count : 256;
        count = count >= 1 ? count : 1;

        NSDictionary *params = @{kCIInputImageKey: ciImage,
                                 kCIInputExtentKey: [CIVector vectorWithCGRect:[ciImage extent]],
                                 @"inputCount": @(256), @"inputScale": @(200)
                                 };
        CIFilter *filter = [CIFilter filterWithName:@"CIAreaHistogram"
                                withInputParameters:params];

        CIImage *outImage = [filter outputImage];
    //---------------------------------------------
        CIContext *context = [CIContext contextWithOptions:nil];
        NSDictionary *params2 = @{
                                  kCIInputImageKey: outImage
                                  };
        CIFilter *filter2 = [CIFilter filterWithName:@"CIHistogramDisplayFilter"
                                 withInputParameters:params2];
        CIImage *outputImage = [filter2 outputImage];
        CGRect outExtent = [outputImage extent];
        CGImageRef cgImage = [context createCGImage:outputImage
                                           fromRect:outExtent];
        UIImage *outImage2 = [UIImage imageWithCGImage:cgImage];
        CGImageRelease(cgImage);
        // resize
        UIImage *resized = [self resizeImage:outImage2
                                 withQuality:kCGInterpolationNone
                                        rate:2.5];
        //Remove the default grey background
        resized = [self removeColorFromImage:resized grayLevel:137];
        dispatch_async(dispatch_get_main_queue(),
        ^{
            self.histogramView.image = resized;
        });

在将其转换为Swift时,我开始出现以下错误:

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

最后一行给了我一个错误:

Argument labels '(cVPixelBuffer:, options:)' do not match any available overloads
//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
    }

相关内容

  • 没有找到相关文章

最新更新