在Objective-C中未被识别的函数

时间:2016-04-17 01:43:14

标签: c++ ios objective-c

我是IOS新手,但我面临的这个问题对我来说似乎不合逻辑。我在ViewController.h和ViewController.mm中声明了一个函数(改为.mm,因为我使用C ++)但是当我在ViewController.mm中调用它时,我得到use of undeclared identifier错误。< / p>

这里是头文件中的声明:

#import <UIKit/UIKit.h>
#import <opencv2/highgui/cap_ios.h>

using namespace cv;

@interface ViewController : UIViewController<CvVideoCameraDelegate>
{
    IBOutlet UIImageView* imageview;
    CvVideoCamera* videoCamera;
    IBOutlet UILabel *label;

}
- (UIImage *) CVMatToImage:(cv::Mat) matrice;

@property (nonatomic, retain) CvVideoCamera* videoCamera;

-(IBAction)cameraStart:(id)sender;
-(IBAction)cameraStop:(id)sender;

@end

和.mm文件中的定义:

- (UIImage *) CVMatToImage:(cv::Mat) matrice
{
    NSData *data = [NSData dataWithBytes:matrice.data length:matrice.elemSize()*matrice.total()];

    CGColorSpaceRef colorSpace;
    CGBitmapInfo bitmapInfo;

    if (matrice.elemSize() == 1) {
        colorSpace = CGColorSpaceCreateDeviceGray();
        bitmapInfo = kCGImageAlphaNone | kCGBitmapByteOrderDefault;
    } else {
        colorSpace = CGColorSpaceCreateDeviceRGB();
        bitmapInfo = kCGBitmapByteOrder32Little | (
                                                   matrice.elemSize() == 3? kCGImageAlphaNone : kCGImageAlphaNoneSkipFirst
                                                   );
    }

    CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);

    // Creating CGImage from cv::Mat
    CGImageRef imageRef = CGImageCreate(
                                        matrice.cols,                 //width
                                        matrice.rows,                 //height
                                        8,                          //bits per component
                                        8 * matrice.elemSize(),       //bits per pixel
                                        matrice.step[0],              //bytesPerRow
                                        colorSpace,                 //colorspace
                                        bitmapInfo,                 // bitmap info
                                        provider,                   //CGDataProviderRef
                                        NULL,                       //decode
                                        false,                      //should interpolate
                                        kCGRenderingIntentDefault   //intent
                                        );

    // Getting UIImage from CGImage
    UIImage *finalImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    CGDataProviderRelease(provider);
    CGColorSpaceRelease(colorSpace);

    return finalImage;


}

但是当我在IBAction中调用它时会出现错误

-(IBAction)cameraStop:(id)sender
{


    [self.videoCamera stop];

    //////////////////////

    // Don't forget to process image here

    //////////////////////
    UIImage  *finalImageToOCR = CVMatToImage(cvMatrice);

    [imageview setImage:finalImageToOCR];
    G8Tesseract *tesseract = [[G8Tesseract alloc] initWithLanguage:@"fra"];
    [tesseract setImage:finalImageToOCR];
    [tesseract recognize];
    NSLog(@"%@", [tesseract recognizedText]);
    label.text = [NSString stringWithFormat:[tesseract recognizedText]];


}

1 个答案:

答案 0 :(得分:3)

您已定义实例方法而非功能。与代码中的其他方法调用一样,您需要使用以下行的方法调用:

UIImage *finalImageToOCR = [<object instance> CVMatToImage:cvMatrice];

或者,如果您想要一个函数,您将声明为:

UIImage *CVMatToImage(cv::Mat matrice) { ... }

在这种情况下,函数无法访问任何实例变量。

HTH