YMCustomCameraViewModel.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //
  2. // YMCustomCameraViewModel.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/3/17.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMCustomCameraViewModel.h"
  9. #import "YMSamplePhotoModel.h"
  10. @interface YMCustomCameraViewModel ()<YMCameraManagerDelegate>
  11. /// 是否切换前后摄像机
  12. @property (nonatomic, assign, readwrite) BOOL isSwitchFrontAndRearCamera;
  13. /// 闪光灯模式
  14. @property (nonatomic, assign, readwrite) YMCaptureFlashMode flashMode;
  15. /// 示例照链接
  16. @property (nonatomic, strong, readwrite) NSString *samplePhotoUrl;
  17. /// 示例照类型
  18. @property (nonatomic, assign, readwrite) NSInteger samplePhotoType;
  19. /// 摄像机管理器
  20. @property (nonatomic, strong, readwrite) YMCameraManager *cameraManager;
  21. @end
  22. @implementation YMCustomCameraViewModel
  23. - (void)ym_initialize{
  24. [super ym_initialize];
  25. self.isSwitchFrontAndRearCamera = YES;
  26. self.flashMode = YMCaptureFlashModeOff;
  27. [self.cameraManager switchCamera:self.isSwitchFrontAndRearCamera didFinishChanceBlock:^{
  28. }];
  29. }
  30. - (void)getSamplePhotoData{
  31. [LCHttpHelper requestWithURLString:CameraAudit parameters:@{} needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
  32. NSDictionary* dict = (NSDictionary*)responseObject;
  33. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  34. if (code == 0) {
  35. YMSamplePhotoModel *model = [YMSamplePhotoModel yy_modelWithJSON:[dict dictionaryValueForKey:@"data" defaultValue:@{}]];
  36. self.samplePhotoUrl = model.action_image;
  37. self.samplePhotoType = model.action_type;
  38. }else{
  39. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  40. }
  41. } failure:^(NSError *error) {
  42. [ZCHUDHelper showTitle:error.localizedDescription];
  43. }];
  44. }
  45. - (void)closeCustomCamera{
  46. CATransition *transition = [CATransition animation];
  47. transition.duration = 0.4f;
  48. transition.type = kCATransitionReveal;
  49. transition.subtype = kCATransitionFromBottom;
  50. transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
  51. [[YMGlobalUtils getCurrentVC].navigationController.view.layer addAnimation:transition forKey:kCATransition];
  52. [[YMGlobalUtils getCurrentVC].navigationController popViewControllerAnimated:NO];
  53. }
  54. - (void)takePhoto{
  55. [self.cameraManager takePhotoWithImageBlock:^(UIImage *originImage, UIImage *scaledImage, UIImage *croppedImage) {
  56. if (croppedImage) {
  57. YMAuthenticationCenterViewModel *authenticationCenterVM = [[YMAuthenticationCenterViewModel alloc]initWithParams:@{
  58. ParamsUrl:self.samplePhotoUrl,
  59. ParamsSubType:@(self.samplePhotoType),
  60. @"photo":originImage
  61. }];
  62. [YMRouter openURL:stringFormat(@"%@%@", YM_ROUTER_URL_PREFIX, YM_ROUTER_AUTHENTICATION_CENTER) withUserInfo:@{
  63. RouterViewModel:authenticationCenterVM
  64. } completion:nil];
  65. }
  66. }];
  67. }
  68. - (void)switchFrontAndRearCamera{
  69. self.isSwitchFrontAndRearCamera = !self.isSwitchFrontAndRearCamera;
  70. [self.cameraManager switchCamera:self.isSwitchFrontAndRearCamera didFinishChanceBlock:^{
  71. }];
  72. }
  73. - (void)switchFlashMode{
  74. WS(weakSelf)
  75. [self.cameraManager switchFlashModeDidFinishChanceBlock:^(YMCaptureFlashMode flashMode) {
  76. weakSelf.flashMode = flashMode;
  77. }];
  78. }
  79. #pragma mark - YMCameraManagerDelegate
  80. - (void)cameraDidFinishFocus{
  81. NSLog(@"对焦结束了");
  82. }
  83. - (void)cameraDidStareFocus{
  84. NSLog(@"开始对焦");
  85. }
  86. - (YMCameraManager *)cameraManager{
  87. if (!_cameraManager) {
  88. _cameraManager = [[YMCameraManager alloc] init];
  89. _cameraManager.delegate = self;
  90. }
  91. return _cameraManager;
  92. }
  93. @end