我不太确定这里发生了什么......相机预览层似乎冻结并且 captureStill... 块永远不会被执行。但是,如果我在开始执行应用程序时关闭闪光灯,它会很好地捕捉图片。还应该注意的是,我遇到了这个线程:在 AVCaptureDevice 上切换 Flash 需要停止和启动 AVCaptureSEssion并决定继续尝试停止会话并再次启动它,它似乎有效。不再冻结,但是在关闭会话并重新打开时,相机会稍微冻结,这是不可接受的。想法?
- (void)cameraButtonPressed{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[device lockForConfiguration:nil];
if ([defaults objectForKey:@"Flash"])
{
SPFlashButton option = [[defaults objectForKey:@"Flash"] intValue];
if (option == SPFlashButtonTypeOff)
{
[device setFlashMode:AVCaptureFlashModeOff];
}
else if (option == SPFlashButtonTypeOn)
{
[device setFlashMode:AVCaptureFlashModeOn];
}
else if (option == SPFlashButtonTypeAuto)
{
[device setFlashMode:AVCaptureFlashModeAuto];
}
}
else
{
[device setFlashMode:AVCaptureFlashModeAuto];
}
[device unlockForConfiguration];
AVCaptureConnection *stillImageConnection = nil;
for (AVCaptureConnection *connection in stillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
stillImageConnection = connection;
break;
}
}
if (stillImageConnection) { break; }
}
UIDeviceOrientation curDeviceOrientation = [[UIDevice currentDevice] orientation];
AVCaptureVideoOrientation avcaptureOrientation = [self avOrientationForDeviceOrientation:curDeviceOrientation];
[stillImageConnection setVideoOrientation:avcaptureOrientation];
[stillImageConnection setVideoScaleAndCropFactor:self.zoomScale];
[stillImageOutput setOutputSettings:[NSDictionary dictionaryWithObject:AVVideoCodecJPEG
forKey:AVVideoCodecKey]];
[stillImageOutput captureStillImageAsynchronouslyFromConnection:stillImageConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
NSData *jpegData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage *image = [UIImage imageWithData:jpegData];
//do stuff with the image
}];
}