123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- //
- // YMCustomCameraViewModel.m
- // MSYOUPAI
- //
- // Created by YoMi on 2024/3/17.
- // Copyright © 2024 MS. All rights reserved.
- //
- #import "YMCustomCameraViewModel.h"
- #import "YMSamplePhotoModel.h"
- @interface YMCustomCameraViewModel ()<YMCameraManagerDelegate>
- /// 是否切换前后摄像机
- @property (nonatomic, assign, readwrite) BOOL isSwitchFrontAndRearCamera;
- /// 闪光灯模式
- @property (nonatomic, assign, readwrite) YMCaptureFlashMode flashMode;
- /// 示例照链接
- @property (nonatomic, strong, readwrite) NSString *samplePhotoUrl;
- /// 示例照类型
- @property (nonatomic, assign, readwrite) NSInteger samplePhotoType;
- /// 摄像机管理器
- @property (nonatomic, strong, readwrite) YMCameraManager *cameraManager;
- @end
- @implementation YMCustomCameraViewModel
- - (void)ym_initialize{
- [super ym_initialize];
- self.isSwitchFrontAndRearCamera = YES;
- self.flashMode = YMCaptureFlashModeOff;
- [self.cameraManager switchCamera:self.isSwitchFrontAndRearCamera didFinishChanceBlock:^{
-
- }];
- }
- - (void)getSamplePhotoData{
- [LCHttpHelper requestWithURLString:CameraAudit parameters:@{} needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
- NSDictionary* dict = (NSDictionary*)responseObject;
- NSInteger code = [[dict objectForKey:@"code"] integerValue];
- if (code == 0) {
- YMSamplePhotoModel *model = [YMSamplePhotoModel yy_modelWithJSON:[dict dictionaryValueForKey:@"data" defaultValue:@{}]];
- self.samplePhotoUrl = model.action_image;
- self.samplePhotoType = model.action_type;
- }else{
- [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
- }
- } failure:^(NSError *error) {
- [ZCHUDHelper showTitle:error.localizedDescription];
- }];
- }
- - (void)closeCustomCamera{
- CATransition *transition = [CATransition animation];
- transition.duration = 0.4f;
- transition.type = kCATransitionReveal;
- transition.subtype = kCATransitionFromBottom;
- transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
- [[YMGlobalUtils getCurrentVC].navigationController.view.layer addAnimation:transition forKey:kCATransition];
- [[YMGlobalUtils getCurrentVC].navigationController popViewControllerAnimated:NO];
- }
- - (void)takePhoto{
- [self.cameraManager takePhotoWithImageBlock:^(UIImage *originImage, UIImage *scaledImage, UIImage *croppedImage) {
- if (croppedImage) {
- YMAuthenticationCenterViewModel *authenticationCenterVM = [[YMAuthenticationCenterViewModel alloc]initWithParams:@{
- ParamsUrl:self.samplePhotoUrl,
- ParamsSubType:@(self.samplePhotoType),
- @"photo":originImage
- }];
- [YMRouter openURL:stringFormat(@"%@%@", YM_ROUTER_URL_PREFIX, YM_ROUTER_AUTHENTICATION_CENTER) withUserInfo:@{
- RouterViewModel:authenticationCenterVM
- } completion:nil];
- }
- }];
- }
- - (void)switchFrontAndRearCamera{
- self.isSwitchFrontAndRearCamera = !self.isSwitchFrontAndRearCamera;
- [self.cameraManager switchCamera:self.isSwitchFrontAndRearCamera didFinishChanceBlock:^{
-
- }];
- }
- - (void)switchFlashMode{
- WS(weakSelf)
- [self.cameraManager switchFlashModeDidFinishChanceBlock:^(YMCaptureFlashMode flashMode) {
- weakSelf.flashMode = flashMode;
- }];
- }
- #pragma mark - YMCameraManagerDelegate
- - (void)cameraDidFinishFocus{
- NSLog(@"对焦结束了");
- }
- - (void)cameraDidStareFocus{
- NSLog(@"开始对焦");
- }
- - (YMCameraManager *)cameraManager{
- if (!_cameraManager) {
- _cameraManager = [[YMCameraManager alloc] init];
- _cameraManager.delegate = self;
- }
- return _cameraManager;
- }
- @end
|