YMCustomCameraViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // YMCustomCameraViewController.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/17.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMCustomCameraViewController.h"
  9. #import "YMCustomCameraViewModel.h"
  10. #import "YMCustomCameraTopFunctionView.h"
  11. #import "YMCustomCameraBottomFunctionView.h"
  12. #import "YMAuthenticationCenterViewController.h"
  13. @interface YMCustomCameraViewController ()
  14. /// 自定义相机VM
  15. @property (nonatomic, strong) YMCustomCameraViewModel *viewModel;
  16. /// 摄像机底部功能视图
  17. @property (nonatomic, strong) YMCustomCameraTopFunctionView *cameraTopFunctionView;
  18. /// 摄像机捕获区域遮罩视图
  19. @property (nonatomic, strong) UIView *cameraCaptureAreaView;
  20. /// 摄像机示例照视图
  21. @property (nonatomic, strong) UIImageView *cameraSamplePhotoView;
  22. /// 摄像机底部功能视图
  23. @property (nonatomic, strong) YMCustomCameraBottomFunctionView *cameraBottomFunctionView;
  24. @end
  25. @implementation YMCustomCameraViewController
  26. @dynamic viewModel;
  27. - (void)viewDidLoad {
  28. [super viewDidLoad];
  29. [self setNavHidden:YES];
  30. self.view.backgroundColor = HexColorFromRGB(0x000000);
  31. }
  32. - (void)viewDidAppear:(BOOL)animated{
  33. [super viewDidAppear:animated];
  34. [self deletUnwantedVC];
  35. }
  36. /** 在页面结束或出现记得开启/停止摄像*/
  37. - (void)viewWillAppear:(BOOL)animated{
  38. [super viewWillAppear:animated];
  39. if (![self.viewModel.cameraManager.session isRunning]) {
  40. [self.viewModel.cameraManager.session startRunning];
  41. }
  42. }
  43. - (void)viewWillDisappear:(BOOL)animated{
  44. [super viewWillDisappear:animated];
  45. if ([self.viewModel.cameraManager.session isRunning]) {
  46. [self.viewModel.cameraManager.session stopRunning];
  47. }
  48. }
  49. - (void)ym_setupViews{
  50. [self.view addSubview:self.cameraTopFunctionView];
  51. [self.view addSubview:self.cameraCaptureAreaView];
  52. [self.view addSubview:self.cameraSamplePhotoView];
  53. [self.view addSubview:self.cameraBottomFunctionView];
  54. [self.view setNeedsUpdateConstraints];
  55. [self.view updateConstraintsIfNeeded];
  56. }
  57. - (void)updateViewConstraints{
  58. [self.cameraTopFunctionView mas_makeConstraints:^(MASConstraintMaker *make) {
  59. make.top.equalTo(self.view).offset(kYMStatusBarHeight);
  60. make.left.equalTo(self.view);
  61. make.right.equalTo(self.view);
  62. make.height.mas_equalTo(adapt(45));
  63. }];
  64. [self.cameraCaptureAreaView mas_makeConstraints:^(MASConstraintMaker *make) {
  65. make.top.equalTo(self.cameraTopFunctionView.mas_bottom);
  66. make.left.equalTo(self.view);
  67. make.right.equalTo(self.view);
  68. make.bottom.equalTo(self.cameraBottomFunctionView.mas_top);
  69. }];
  70. [self.cameraSamplePhotoView mas_makeConstraints:^(MASConstraintMaker *make) {
  71. make.left.equalTo(self.view);
  72. make.bottom.equalTo(self.cameraBottomFunctionView.mas_top);
  73. make.width.height.mas_equalTo(adapt(120));
  74. }];
  75. [self.cameraBottomFunctionView mas_makeConstraints:^(MASConstraintMaker *make) {
  76. make.left.equalTo(self.view);
  77. make.right.equalTo(self.view);
  78. make.bottom.equalTo(self.view);
  79. make.height.mas_equalTo(adapt(100));
  80. }];
  81. [super updateViewConstraints];
  82. }
  83. - (void)ym_bindViewModel{
  84. [self.cameraTopFunctionView ym_bindViewModel:self.viewModel];
  85. [self.cameraBottomFunctionView ym_bindViewModel:self.viewModel];
  86. [self.viewModel getSamplePhotoData];
  87. [self.viewModel.cameraManager configureWithParentLayer:self.cameraCaptureAreaView];
  88. @weakify(self)
  89. [[[[RACObserve(self.viewModel, samplePhotoUrl) distinctUntilChanged] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSString * samplePhotoUrl) {
  90. @strongify(self)
  91. [self.cameraSamplePhotoView sd_setImageWithURL:[LCTools getImageUrlWithAddress:samplePhotoUrl] placeholderImage:[UIImage new] options:SDWebImageRefreshCached];
  92. }];
  93. }
  94. #pragma mark - 删除不需要的视图控制器
  95. - (void)deletUnwantedVC{
  96. for (UIViewController *vc in self.navigationController.viewControllers) {
  97. if ([vc isKindOfClass:[YMAuthenticationCenterViewController class]]) {
  98. [vc removeFromParentViewController];
  99. }
  100. }
  101. }
  102. /// 点击对焦
  103. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  104. UITouch *touch = [touches anyObject];
  105. CGPoint point = [touch locationInView:self.view];
  106. [self.viewModel.cameraManager focusInPoint:point];
  107. }
  108. - (YMCustomCameraTopFunctionView *)cameraTopFunctionView{
  109. if (!_cameraTopFunctionView) {
  110. _cameraTopFunctionView = [[YMCustomCameraTopFunctionView alloc]init];
  111. }
  112. return _cameraTopFunctionView;
  113. }
  114. - (UIView *)cameraCaptureAreaView{
  115. if (!_cameraCaptureAreaView) {
  116. _cameraCaptureAreaView = [[UIView alloc]initWithFrame:CGRectMake(0, kYMStatusBarHeight, kFrameWidth, kFrameHeight - kYMStatusBarHeight - adapt(45) - adapt(100))];
  117. }
  118. return _cameraCaptureAreaView;
  119. }
  120. - (UIImageView *)cameraSamplePhotoView{
  121. if (!_cameraSamplePhotoView) {
  122. _cameraSamplePhotoView = [[UIImageView alloc]init];
  123. }
  124. return _cameraSamplePhotoView;
  125. }
  126. - (YMCustomCameraBottomFunctionView *)cameraBottomFunctionView{
  127. if (!_cameraBottomFunctionView) {
  128. _cameraBottomFunctionView = [[YMCustomCameraBottomFunctionView alloc]init];
  129. }
  130. return _cameraBottomFunctionView;
  131. }
  132. @end