IOS调用函数给出错误

标签 ios objective-c

我正在使用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/

相关文章:

ios - 如何在运行时更改 Form.Quality 属性(适用于 iOS)

ios - 尝试访问 PFUser 子类的动态属性时出错

ios - 将 UITableView 滑动到屏幕上,继续滑动?

ios - Jenkins 编译 .xib 时失败

ios - 如何在 Objective-C 中将照片分享到 Facebook?

ios - 如何使用 AWS Rekognition 在 Swift 3 中检测图像标签和人脸

ios - 不支持指定多个 post_install Hook 的无效 Podfile 文件

ios - NSURLRequest 在 iphone 5s 和更新版本上给出正确结果但在 iphone 4s 和 5 上失败

ios - 在居中的 UIButton 上左对齐图像

ios - 您能否使 Xcode 5.1 目标依赖项仅构建有效的体系结构?