如何关注captureStillImageAsynchronouslyFromConnection方法

时间:2012-11-23 12:37:30

标签: ios image camera avcapturesession

我想在iOS上拍照。

我坚持使用方法 captureStillImageAsynchronouslyFromConnection 拍摄图像。

我实现了这个方法并且它可以工作,但我的问题是它立即拍摄图像并且不等待首先聚焦它。

我真的试图找到答案,但我没有。

我已经实现了这样的预览视频:

session = [[AVCaptureSession alloc] init];
session.sessionPreset = AVCaptureSessionPresetHigh;
AVCaptureVideoPreviewLayer *captureVideoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
captureVideoPreviewLayer.frame = cameraImage.frame;
captureVideoPreviewLayer.bounds = cameraImage.bounds;
captureVideoPreviewLayer.orientation = AVCaptureVideoOrientationLandscapeRight;
[cameraImage.layer addSublayer:captureVideoPreviewLayer];
 device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


 NSError *error = nil;


if ([session canSetSessionPreset:AVCaptureSessionPreset1920x1080]) {
    session.sessionPreset = AVCaptureSessionPreset1920x1080;
}

[session addInput:input];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey,nil];
[stillImageOutput setOutputSettings:outputSettings];

[session addOutput:stillImageOutput];

[session startRunning];

按下按钮我有:

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


[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
 {
     CFDictionaryRef exifAttachments = CMGetAttachment( imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
     if (exifAttachments)
     {
     }
     else{}


     // TAKE IMAGE


    if ([device lockForConfiguration:&error]) {
        [device setFocusPointOfInterest:CGPointMake(0.5f, 0.f)];
        [device setExposurePointOfInterest:CGPointMake(0.5f, 0.f)];

        [device setFocusMode:AVCaptureFocusModeAutoFocus];
        [device unlockForConfiguration];
    }
    NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
     UIImage *image = [[UIImage alloc] initWithData:imageData];
     UIImage *rotatedImage = [[UIImage alloc]initWithCGImage:image.CGImage scale:1.0f orientation:UIImageOrientationDown];

     [session stopRunning];

但不幸的是,拍摄的照片没有集中注意力。

有人可以帮助我。

马尔科

1 个答案:

答案 0 :(得分:1)

您希望观察调整焦点值并在完成后拍摄照片。首先,在我通常尝试捕获图像之前,我检查设备是否正在聚焦并添加观察者,如果是:

if ([device isAdjustingFocus])
{
    [device addObserver: self forKeyPath: @"adjustingFocus" options: NSKeyValueObservingOptionNew context:nil ];
}
else
{
    // You can just take the image if the device isn't adjusting
}

然后创建一个观察值变化的方法:

-(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if ([keyPath isEqualToString: @"adjustingFocus"])
    {
        BOOL adjustingFocus = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
        NSLog(@"Is adjusting focus? %@", adjustingFocus ? @"YES" : @"NO" );

        //
        // If not adjusting focus then call
        // captureStillImageAsynchronouslyFromConnection here
        if (adjustingFocus == NO)
        {
            // Remove the observer when we're finished
            [device removeObserver: self forKeyPath: @"adjustingFocus" ];

        }
    }
}

希望有帮助...