草庐IT

iOS:invocationWithMethodSignature 方法签名参数问题

coder 2024-01-20 原文

我正在开发一个应用程序,我在其中使用 NSInvocation 作为以下代码来处理 NSTimer。我收到以下代码的 Sigbart 错误 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+[NSInvocation invocationWithMethodSignature:]: 方法签名参数不能为零” *** 首先抛出调用栈

我正在尝试识别此崩溃,但无法识别。有人可以帮我解决这个问题吗?

代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveImageToPhotoAlbum) name:kImageCapturedSuccessfully object:nil];

    NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(capturePicture:)]];
    [myInvocation setSelector:@selector(capturePicture:)];
    [myInvocation setTarget:self];
    [myInvocation retainArguments];

    [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:myInvocation repeats:NO];




   -(void) capturePicture :(id) sender
{
    NSLog(@"capTureAutoPicture:Scanning image at interval");

    // Get all cameras in the application and find the frontal camera.
    AVCaptureDevice *backCamera;
    NSArray *allCameras = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];

    // Find the back camera.
    for ( int i = 0; i < allCameras.count; i++ ) {
        AVCaptureDevice *camera = [allCameras objectAtIndex:i];

        if ( camera.position == AVCaptureDevicePositionBack ) {
            backCamera = camera;
        }
    }

    // If we did not find the camera then do not take picture.
    if ( backCamera != nil ) {
        // Start the process of getting a picture.
        AVCaptureSession *session = [[AVCaptureSession alloc] init];

        // Setup instance of input with back camera and add to session.
        NSError *error;
        AVCaptureDeviceInput *input =
        [AVCaptureDeviceInput deviceInputWithDevice:backCamera error:&error];

        if ( !error && [session canAddInput:input] ) {
            // Add frontal camera to this session.
            [session addInput:input];

            // We need to capture still image.
            AVCaptureStillImageOutput *output = [[AVCaptureStillImageOutput alloc] init];

            // Captured image. settings.
            [output setOutputSettings:
             [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]];

            if ( [session canAddOutput:output] ) 
            {

                [session addOutput:output];

                AVCaptureConnection *videoConnection = nil;
                for (AVCaptureConnection *connection in output.connections) {
                    for (AVCaptureInputPort *port in [connection inputPorts]) {
                        if ( [[port mediaType] isEqual:AVMediaTypeVideo] ) 
                        {
                            videoConnection = connection;
                            break;
                        }
                    }
                    if (videoConnection) 
                    { 
                        break; 
                    }
                }

                // Finally take the picture
                if ( videoConnection ) 
                {
                    [session startRunning];

                    [output captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) 
                     {
                         if (imageDataSampleBuffer != NULL) 
                         {
                             NSData *imageData = [AVCaptureStillImageOutput 
                                                  jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
                             UIImage *photo = [[UIImage alloc] initWithData:imageData];

                             //UIImageWriteToSavedPhotosAlbum(photo, nil, nil, nil);
                             [photo release];

                             [[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];

                             [session stopRunning];

                         }

                     }];

                }
            }
            [output release];
        }
    }
}
- (void)saveImageToPhotoAlbum
{
    UIImageWriteToSavedPhotosAlbum([self stillImage], self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    [self dismissModalViewControllerAnimated:YES];
}

更新代码:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveImageToPhotoAlbum) name:kImageCapturedSuccessfully object:nil];

    /*NSInvocation *myInvocation = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(capTureAutoPicture)]];
    [myInvocation setSelector:@selector(capTureAutoPicture)];
    [myInvocation setTarget:self];
    [NSTimer scheduledTimerWithTimeInterval:5.0 invocation:myInvocation repeats:NO];*/

    [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(capTureAutoPicture:) userInfo:nil repeats:NO];

最佳答案

抛出的异常非常清楚,参数不能为 nil。

以下是 NSInvocation 的工作示例:

NSMethodSignature *methodSignature = [[_filter class] instanceMethodSignatureForSelector:@selector()];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
[invocation setTarget:_filter];
[invocation setSelector:selector];
[invocation setArgument:&value atIndex:2];
[invocation invoke];

我还建议您看看 answer to this question这很好地解释了 NSInvocation 是如何工作的

关于iOS:invocationWithMethodSignature 方法签名参数问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14344438/

有关iOS:invocationWithMethodSignature 方法签名参数问题的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  4. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  5. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  6. Ruby 方法() 方法 - 2

    我想了解Ruby方法methods()是如何工作的。我尝试使用“ruby方法”在Google上搜索,但这不是我需要的。我也看过ruby​​-doc.org,但我没有找到这种方法。你能详细解释一下它是如何工作的或者给我一个链接吗?更新我用methods()方法做了实验,得到了这样的结果:'labrat'代码classFirstdeffirst_instance_mymethodenddefself.first_class_mymethodendendclassSecond使用类#returnsavailablemethodslistforclassandancestorsputsSeco

  7. ruby - 通过 rvm 升级 ruby​​gems 的问题 - 2

    尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub

  8. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  9. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  10. ruby - Highline 询问方法不会使用同一行 - 2

    设置:狂欢ruby1.9.2高线(1.6.13)描述:我已经相当习惯在其他一些项目中使用highline,但已经有几个月没有使用它了。现在,在Ruby1.9.2上全新安装时,它似乎不允许在同一行回答提示。所以以前我会看到类似的东西:require"highline/import"ask"Whatisyourfavoritecolor?"并得到:Whatisyourfavoritecolor?|现在我看到类似的东西:Whatisyourfavoritecolor?|竖线(|)符号是我的终端光标。知道为什么会发生这种变化吗? 最佳答案

随机推荐