captureStillImageAsynchronouslyFromConnection延迟

时间:2015-01-08 14:44:57

标签: ios camera

我使用下面的代码捕获图像。我在一个带有3秒计时器的循环中调用了这个例程,所以我每隔3秒捕获一个图像。这通常可以正常工作,但有时执行captureStillImageAsynchronouslyFromConnection()方法,程序只需在那里停留10-20秒,没有明显的事情发生。然后它恢复正常操作,每3秒捕获一次图像。什么会导致这种延迟发生?

- (void)captureStillImage
{
    NSLog(@"about to request a capture from: %@", [self stillImageOutput]);

    if ( videoConnection ) {

        waitingForCapture = true;

        NSLog(@"requesting a capture from: %@", [self stillImageOutput]);
        [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                         completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {

            if ( imageSampleBuffer ) {

                CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
                if (exifAttachments) {
                    NSLog(@"attachements: %@", exifAttachments);
                } else {
                    NSLog(@"no attachments");
                }

                NSLog(@"name: %@", [currentCaptureDevice localizedName]);

                NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
                UIImage *image = [[UIImage alloc] initWithData:imageData];
                [self setStillImage:image];


                NSDictionary *dict = (__bridge NSDictionary*)exifAttachments;
                NSString *value = [dict objectForKey:@"PixelXDimension"];
                [self setImageWidth:[NSNumber numberWithInt:[value intValue]]];

                value = [dict objectForKey:@"PixelYDimension"];
                [self setImageHeight:[NSNumber numberWithInt:[value intValue]]];

                value = [dict objectForKey:@"BrightnessValue"];
                [self setImageBrightnessValue:[NSNumber numberWithFloat:[value floatValue]]];

                value = [dict objectForKey:@"ShutterSpeedValue"];
                double val = [value doubleValue];
                val = 1.0 / pow(2.0, val);
                [self setImageExposureTime:[NSNumber numberWithDouble:val]];

                value = [dict objectForKey:@"ApertureValue"];
                [self setImageApertureValue:[NSNumber numberWithFloat:[value floatValue]]];

                NSArray *values = [dict objectForKey:@"ISOSpeedRatings"];
                [self setImageISOSpeedRatings:[NSNumber numberWithInt:[ [values objectAtIndex:0] intValue]]];


                NSLog(@"r/g/b gains = %.2lf/%.2lf/%.2lf",
                      currentCaptureDevice.deviceWhiteBalanceGains.redGain,
                      currentCaptureDevice.deviceWhiteBalanceGains.greenGain,
                      currentCaptureDevice.deviceWhiteBalanceGains.blueGain);


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

            } else {
                NSLog(@"imageSampleBuffer = NULL");
            }

            waitingForCapture = false;

         }];
    } else {
        NSLog(@"can't capture from: videoConnection");
    }

}

这是我如何开球:

-(void)startContinuousPhotometry
{
    progressView.progress = 0.0f;
    if ( ![self checkCameraStatus]) {
        return;
    }

    continueRunning = true;
    backgroundQueue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);
    dispatch_set_target_queue(backgroundQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
    [self process];

}


-(void)process {
    dispatch_async(backgroundQueue, ^(void) {
        [self live];
    });
}


-(void)live
{

    while ( continueRunning ) {

        if ( [self isCameraChangingSettings] ) {
            NSLog(@"adjusting exposure");
        } else {
            NSLog(@"NOT adjusting exposure");
        }

        if ( ![[self captureManager] getWaitingForCapture]) {
            [[self captureManager] captureStillImage];
            [NSThread sleepForTimeInterval:LOOP_PAUSE_TIME_S];
        } else {
            [NSThread sleepForTimeInterval:1]; // wait 1 second
            NSLog(@"waiting for getWaitingForCapture");
        }
    }

}

0 个答案:

没有答案
相关问题