我正在使用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);
}
}];
}
从我的 View Controller 中,我调用这个函数,如下所示:
UIImage *img;
NSError *e;
[_scanner captureStillImage:img :e];
但给我错误:
No visible @interface for 'MTBBarcodeScanner' declares the selector 'captureStillImage::
如何将此函数称为我的 UIViewcontroller
子类?
最佳答案
您的 block 的语法不正确。应该是这样的:
[_scanner captureStillImage:^(UIImage *image, NSError *error) {
}];
此外,这是一个回调函数,您不应该向其中提供参数,这些参数是从它返回的。
如果您希望在回调之外使用代表回调函数返回值的变量,则需要声明 __block
变量。
__block UIImage* img;
__block NSError* e;
[_scanner captureStillImage:^(UIImage *image, NSError *error) {
img = image;
e = error;
}];
关于IOS调用函数给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41618996/