我开发了一个 iOS 应用程序,其中有一个相机可以拍照。我已经使用 AVCaptureSession
来实现相机并且它工作正常。然后我添加了一个 UINavigationController
以转到预览 View (包含拍摄照片的 View )并返回到相机 View 。导航工作没有任何问题,问题是当我回到相机 View 时,相机馈送不再工作,它被卡住了。有人可以帮我解决这个问题吗?我附上了我的代码和 Storyboard的图像。
我用来实现相机馈送的一段代码
@implementation CameraViewController{
UIView *view_cameraFeed;
AVCaptureSession *session;
AVCaptureStillImageOutput *stillImageOutput;
}
- (void)viewDidLoad {
[super viewDidLoad];
view_cameraFeed = [[UIView alloc]initWithFrame:CGRectMake(X,Y, WIDTH,HEIGHT)];
[self.view addSubview:view_cameraFeed];
}
- (void)viewWillAppear:(BOOL)animated{
session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];
AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
if ([session canAddInput:deviceInput]) {
[session addInput:deviceInput];
}
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = self.view.layer;
[rootLayer setMasksToBounds:YES];
CGRect frame = view_cameraFeed.frame;
[previewLayer setFrame:frame];
[rootLayer insertSublayer:previewLayer atIndex:0];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey,nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
[session startRunning];
}
一段用于导航到预览 View 的代码
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
UIViewController *CameraPreviewViewController = [storyboard instantiateViewControllerWithIdentifier:@"CameraPreviewViewController"];
[self.navigationController pushViewController:CameraPreviewViewController animated:YES];
用于返回相机源 View 的代码片段
[self.navigationController popViewControllerAnimated:YES];
这是我的 Storyboard
最佳答案
最后我找到了解决这个问题的方法。很抱歉延迟发布答案。
如果我们在 viewWillAppear 中初始化 session 和其余的东西(除了“[session startRunning]”),当我们导航回 View 时,这些步骤将运行不止一次。这就是它卡住的原因。
我只是将 viewWillAppear 中除“[session startRunning]”之外的所有行移至 viewDidLoad。这样这些行只会运行一次。
这解决了我的问题,这是工作代码。
我用来实现相机馈送的一段代码
@implementation CameraViewController{
UIView *view_cameraFeed;
AVCaptureSession *session;
AVCaptureStillImageOutput *stillImageOutput;
}
- (void)viewDidLoad {
[super viewDidLoad];
view_cameraFeed = [[UIView alloc]initWithFrame:CGRectMake(X,Y, WIDTH,HEIGHT)];
[self.view addSubview:view_cameraFeed];
session = [[AVCaptureSession alloc] init];
[session setSessionPreset:AVCaptureSessionPresetPhoto];
AVCaptureDevice *inputDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error;
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:inputDevice error:&error];
if ([session canAddInput:deviceInput]) {
[session addInput:deviceInput];
}
AVCaptureVideoPreviewLayer *previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:session];
[previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
CALayer *rootLayer = self.view.layer;
[rootLayer setMasksToBounds:YES];
CGRect frame = view_cameraFeed.frame;
[previewLayer setFrame:frame];
[rootLayer insertSublayer:previewLayer atIndex:0];
stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary *outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG, AVVideoCodecKey,nil];
[stillImageOutput setOutputSettings:outputSettings];
[session addOutput:stillImageOutput];
}
- (void)viewWillAppear:(BOOL)animated{
[session startRunning];
}
所有其他代码与问题中的相同
关于ios - 向后导航时相机进给卡住 - ios,objective-c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31935921/