我正在使用MTBBarcodeScanner
interface来实施条形码扫描程序
我需要在我的代码中获取扫描仪的静止图像,所以我试着调用函数:
- (void)captureStillImage:(void (^)(UIImage *image, NSError *error))captureBlock {
if ([self isCapturingStillImage]) {
if (captureBlock) {
NSError *error = [NSError errorWithDomain:kErrorDomain
code:kErrorCodeStillImageCaptureInProgress
userInfo:@{NSLocalizedDescriptionKey : @"Still image capture is already in progress. Check with isCapturingStillImage"}];
captureBlock(nil, error);
}
return;
}
AVCaptureConnection *stillConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
if (stillConnection == nil) {
if (captureBlock) {
NSError *error = [NSError errorWithDomain:kErrorDomain
code:kErrorCodeSessionIsClosed
userInfo:@{NSLocalizedDescriptionKey : @"AVCaptureConnection is closed"}];
captureBlock(nil, error);
}
return;
}
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:stillConnection
completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (error) {
captureBlock(nil, error);
return;
}
NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:jpegData];
if (captureBlock) {
captureBlock(image, nil);
}
}];
}
从我的viewcontroller我调用这个函数:
UIImage *img;
NSError *e;
[_scanner captureStillImage:img :e];
但是给我错误:
对于MTBBarcodeScanner'没有可见的@interface?声明选择器' captureStillImage ::
如何将此函数称为my UIViewcontroller
子类?
答案 0 :(得分:1)
您的块的语法不正确。它应该如下:
[_scanner captureStillImage:^(UIImage *image, NSError *error) {
}];
此外,这是一个回调函数,你不应该向它提供参数,这些参数是从它返回的。
如果您希望在回调之外使用表示回调函数返回值的变量,则需要声明__block
个变量。
__block UIImage* img;
__block NSError* e;
[_scanner captureStillImage:^(UIImage *image, NSError *error) {
img = image;
e = error;
}];