ksi*_*iit 5 iphone objective-c ios avcapturesession
我在尝试拍摄照片时遇到了崩溃(前置摄像头),只有在用户使用图片模式拍摄单独的应用视频时才会失败.如果用户没有图片中的视频,一切正常.崩溃发生在这一行:
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
Run Code Online (Sandbox Code Playgroud)
有错误
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[AVCaptureStillImageOutput captureStillImageAsynchronouslyFromConnection:completionHandler:] - inconsistent state.'
Run Code Online (Sandbox Code Playgroud)
我尝试检查手机是否在使用图片模式时无法拍摄照片,但默认的iOS相机应用程序可以拍摄照片(尽管它可能使用不同的拍照方法).stillImageOutput
而videoConnection
似乎是树立优良的和不为零.
以下是导致此崩溃的代码,以防它有所帮助.
avCaptureSession = [[AVCaptureSession alloc] init];
AVCaptureDevice* cameraDevice = [GS60_FriendFeed_ScreenshotSelfie_Preview_View frontFacingCameraIfAvailable];
avCaptureSession.sessionPreset = avCaptureSessionPresetString;
NSError *error = nil;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:cameraDevice error:&error];
if (!input) {
NSLog(@"ERROR: trying to open camera: %@", error);
}
[avCaptureSession addInput:input];
AVCaptureStillImageOutput* stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys: AVVideoCodecJPEG, AVVideoCodecKey, nil];
[stillImageOutput setOutputSettings:outputSettings];
[avCaptureSession addOutput:stillImageOutput];
[avCaptureSession startRunning];
Run Code Online (Sandbox Code Playgroud)
然后
AVCaptureConnection* videoConnection = nil;
AVCaptureStillImageOutput* stillImageOutput = [[avCaptureSession outputs] objectAtIndex:0];
for (AVCaptureConnection* connection in stillImageOutput.connections) {
for (AVCaptureInputPort *port in [connection inputPorts]) {
if ([[port mediaType] isEqual:AVMediaTypeVideo] ) {
videoConnection = connection;
break;
}
}
if (videoConnection) { break; }
}
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
AVCaptureVideoOrientation avcaptureOrientation = AVCaptureVideoOrientationPortrait;
if(orientation == UIInterfaceOrientationUnknown) {
avcaptureOrientation = AVCaptureVideoOrientationPortrait;
} else if(orientation == UIInterfaceOrientationPortrait) {
avcaptureOrientation = AVCaptureVideoOrientationPortrait;
} else if(orientation == UIInterfaceOrientationPortraitUpsideDown) {
avcaptureOrientation = AVCaptureVideoOrientationPortraitUpsideDown;
} else if(orientation == UIInterfaceOrientationLandscapeLeft) {
avcaptureOrientation = AVCaptureVideoOrientationLandscapeLeft;
} else if(orientation == UIInterfaceOrientationLandscapeRight) {
avcaptureOrientation = AVCaptureVideoOrientationLandscapeRight;
}
[videoConnection setVideoOrientation:avcaptureOrientation];
//this line flips the image so it uses exactly what the preview shows
[videoConnection setVideoMirrored:YES];
[stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
...
Run Code Online (Sandbox Code Playgroud)
我希望能够拍摄照片,但是如果图片中的图片是开放的那样是不可能的,那么知道如何检测到我们将无法拍摄它仍然会有所帮助.
谢谢你的帮助.
确保连接存在并启用
if (!videoConnection || !videoConnection.enabled || !videoConnection.active) {
// Raise error here / warn user...
return;
}
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
AVCaptureVideoOrientation avcaptureOrientation = AVCaptureVideoOrientationPortrait;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1679 次 |
最近记录: |