向后导航时相机画面卡住 - ios、objective-c

Camera feed get stuck when navigate back - ios, objective-c

我开发了一个 iOS 应用程序,其中有一个相机可以拍照。我已经使用 AVCaptureSession 来实现相机并且它工作正常。然后我添加了一个 UINavigationController 以转到预览视图(包含已拍摄照片的视图)并返回到相机视图。导航工作没有任何问题,问题是当我回到相机视图时,相机馈送不再工作,它被卡住了。有人可以帮我解决这个问题吗?我附上了我的代码和故事板的图像。

我用来实现相机馈送的一段代码

@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];
}

一段用于导航到预览视图的代码

 UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

 UIViewController *CameraPreviewViewController = [storyboard instantiateViewControllerWithIdentifier:@"CameraPreviewViewController"];

[self.navigationController pushViewController:CameraPreviewViewController animated:YES];

一段用于返回相机源视图的代码

[self.navigationController popViewControllerAnimated:YES];

这是我的故事板

我终于找到了解决这个问题的方法。很抱歉延迟 post 回答。

如果我们在 viewWillAppear 中初始化会话和其他内容(“[session startRunning]”除外),当我们导航回视图时,这些步骤将 运行 不止一次。这就是它卡住的原因。

我只是将 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];
}

其他代码同题