I have this code snippet to display a histogram in Objective-C, which works fine. I am, however, having a hard time converting it to 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;
});
While converting it to Swift, I started getting these errors:
在将其转换为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]))
That last line gives me the error:
最后一行给了我错误:
Argument labels '(cVPixelBuffer:, options:)' do not match any available overloads
1 个解决方案
#1
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
}
#1
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
}