フロントカメラを使用してユーザーに「ミラー」を表示する「ミラー」のようなビューをアプリで作成しました。私が抱えている問題は、このコードに何週間も触れていないことです (そして、それは機能しました) が、今はもう一度テストしていますが、機能していません。コードは以前と同じで、エラーは発生せず、ストーリーボードのビューは以前とまったく同じです。何が起こっているのかわからないので、このウェブサイトが役立つことを願っていました.
これが私のコードです:
if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
//If the front camera is available, show the camera
AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureOutput *output = [[AVCaptureStillImageOutput alloc] init];
[session addOutput:output];
//Setup camera input
NSArray *possibleDevices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
//You could check for front or back camera here, but for simplicity just grab the first device
AVCaptureDevice *device = [possibleDevices objectAtIndex:1];
NSError *error = nil;
// create an input and add it to the session
AVCaptureDeviceInput* input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error]; //Handle errors
//set the session preset
session.sessionPreset = AVCaptureSessionPresetHigh; //Or other preset supported by the input device
[session addInput:input];
AVCaptureVideoPreviewLayer *previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:session];
//Now you can add this layer to a view of your view controller
[cameraView.layer addSublayer:previewLayer];
previewLayer.frame = self.cameraView.bounds;
[session startRunning];
if ([session isRunning]) {
NSLog(@"The session is running");
}
if ([session isInterrupted]) {
NSLog(@"The session has been interupted");
}
} else {
//Tell the user they don't have a front facing camera
}
よろしくお願いします。