我正在尝试在 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];
但遗憾的是拍摄的照片没有对焦。
有人可以帮我吗。
马尔科