123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- //
- // YMCustomCameraViewController.m
- // MSYOUPAI
- //
- // Created by YoMi on 2024/3/17.
- // Copyright © 2024 MS. All rights reserved.
- //
- #import "YMCustomCameraViewController.h"
- #import "YMCustomCameraViewModel.h"
- #import "YMCustomCameraTopFunctionView.h"
- #import "YMCustomCameraBottomFunctionView.h"
- #import "YMAuthenticationCenterViewController.h"
- @interface YMCustomCameraViewController ()
- /// 自定义相机VM
- @property (nonatomic, strong) YMCustomCameraViewModel *viewModel;
- /// 摄像机底部功能视图
- @property (nonatomic, strong) YMCustomCameraTopFunctionView *cameraTopFunctionView;
- /// 摄像机捕获区域遮罩视图
- @property (nonatomic, strong) UIView *cameraCaptureAreaView;
- /// 摄像机示例照视图
- @property (nonatomic, strong) UIImageView *cameraSamplePhotoView;
- /// 摄像机底部功能视图
- @property (nonatomic, strong) YMCustomCameraBottomFunctionView *cameraBottomFunctionView;
- @end
- @implementation YMCustomCameraViewController
- @dynamic viewModel;
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self setNavHidden:YES];
- self.view.backgroundColor = HexColorFromRGB(0x000000);
- }
- - (void)viewDidAppear:(BOOL)animated{
- [super viewDidAppear:animated];
- [self deletUnwantedVC];
- }
- /** 在页面结束或出现记得开启/停止摄像*/
- - (void)viewWillAppear:(BOOL)animated{
- [super viewWillAppear:animated];
- if (![self.viewModel.cameraManager.session isRunning]) {
- [self.viewModel.cameraManager.session startRunning];
- }
- }
- - (void)viewWillDisappear:(BOOL)animated{
- [super viewWillDisappear:animated];
- if ([self.viewModel.cameraManager.session isRunning]) {
- [self.viewModel.cameraManager.session stopRunning];
- }
- }
- - (void)ym_setupViews{
- [self.view addSubview:self.cameraTopFunctionView];
- [self.view addSubview:self.cameraCaptureAreaView];
- [self.view addSubview:self.cameraSamplePhotoView];
- [self.view addSubview:self.cameraBottomFunctionView];
- [self.view setNeedsUpdateConstraints];
- [self.view updateConstraintsIfNeeded];
- }
- - (void)updateViewConstraints{
-
- [self.cameraTopFunctionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.view).offset(kYMStatusBarHeight);
- make.left.equalTo(self.view);
- make.right.equalTo(self.view);
- make.height.mas_equalTo(adapt(45));
- }];
-
- [self.cameraCaptureAreaView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.top.equalTo(self.cameraTopFunctionView.mas_bottom);
- make.left.equalTo(self.view);
- make.right.equalTo(self.view);
- make.bottom.equalTo(self.cameraBottomFunctionView.mas_top);
- }];
-
- [self.cameraSamplePhotoView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.view);
- make.bottom.equalTo(self.cameraBottomFunctionView.mas_top);
- make.width.height.mas_equalTo(adapt(120));
- }];
-
- [self.cameraBottomFunctionView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.left.equalTo(self.view);
- make.right.equalTo(self.view);
- make.bottom.equalTo(self.view);
- make.height.mas_equalTo(adapt(100));
- }];
-
-
- [super updateViewConstraints];
-
- }
- - (void)ym_bindViewModel{
- [self.cameraTopFunctionView ym_bindViewModel:self.viewModel];
- [self.cameraBottomFunctionView ym_bindViewModel:self.viewModel];
-
- [self.viewModel getSamplePhotoData];
-
- [self.viewModel.cameraManager configureWithParentLayer:self.cameraCaptureAreaView];
-
- @weakify(self)
- [[[[RACObserve(self.viewModel, samplePhotoUrl) distinctUntilChanged] deliverOnMainThread] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSString * samplePhotoUrl) {
- @strongify(self)
- [self.cameraSamplePhotoView sd_setImageWithURL:[LCTools getImageUrlWithAddress:samplePhotoUrl] placeholderImage:[UIImage new] options:SDWebImageRefreshCached];
- }];
- }
- #pragma mark - 删除不需要的视图控制器
- - (void)deletUnwantedVC{
- for (UIViewController *vc in self.navigationController.viewControllers) {
- if ([vc isKindOfClass:[YMAuthenticationCenterViewController class]]) {
- [vc removeFromParentViewController];
- }
- }
- }
- /// 点击对焦
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
- UITouch *touch = [touches anyObject];
- CGPoint point = [touch locationInView:self.view];
- [self.viewModel.cameraManager focusInPoint:point];
- }
- - (YMCustomCameraTopFunctionView *)cameraTopFunctionView{
- if (!_cameraTopFunctionView) {
- _cameraTopFunctionView = [[YMCustomCameraTopFunctionView alloc]init];
- }
- return _cameraTopFunctionView;
- }
- - (UIView *)cameraCaptureAreaView{
- if (!_cameraCaptureAreaView) {
- _cameraCaptureAreaView = [[UIView alloc]initWithFrame:CGRectMake(0, kYMStatusBarHeight, kFrameWidth, kFrameHeight - kYMStatusBarHeight - adapt(45) - adapt(100))];
- }
- return _cameraCaptureAreaView;
- }
- - (UIImageView *)cameraSamplePhotoView{
- if (!_cameraSamplePhotoView) {
- _cameraSamplePhotoView = [[UIImageView alloc]init];
- }
- return _cameraSamplePhotoView;
- }
- - (YMCustomCameraBottomFunctionView *)cameraBottomFunctionView{
- if (!_cameraBottomFunctionView) {
- _cameraBottomFunctionView = [[YMCustomCameraBottomFunctionView alloc]init];
- }
- return _cameraBottomFunctionView;
- }
- @end
|