我在我的应用程序中创建了一个类似“镜子”的视图,它使用前置摄像头向用户显示“镜子”。我遇到的问题是我已经好几周没有接触过这段代码了(当时它确实有效),但现在我再次对其进行测试,但它不起作用。代码和之前一样,没有报错,storyboard中的view和之前一模一样。我不知道发生了什么,所以我希望这个网站会有所帮助。
这是我的代码:
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
}
提前谢谢你。