2

我正在开发一个应用程序,我使用 NSInvocation 处理 NSTimer,如下面的代码。我收到以下代码的 Sigbart 错误*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[NSInvocation invocationWithMethodSignature:]: method signature argument cannot be nil' *** First throw call stack

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

代码:

[[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];
4

2 回答 2

2

抛出的异常很清楚,参数不能为零。

以下是 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];

我还建议您看一下这个问题的答案,它很好地解释了 NSInvocation 的工作原理

于 2013-01-15T20:31:09.093 回答
0

我修好了它。问题是,我没有添加“didFinishSavingWithError”而是调用它。添加此定义后,它起作用了。

于 2013-01-16T05:27:57.467 回答