diff --git a/plugin.xml b/plugin.xml index c73a790..dbe0dee 100644 --- a/plugin.xml +++ b/plugin.xml @@ -46,6 +46,8 @@ + + diff --git a/src/ios/SGRecord/SGRecordManager.m b/src/ios/SGRecord/SGRecordManager.m index 486e164..159ca05 100644 --- a/src/ios/SGRecord/SGRecordManager.m +++ b/src/ios/SGRecord/SGRecordManager.m @@ -538,7 +538,7 @@ typedef void(^PropertyChangeBlock)(AVCaptureDevice *captureDevice); - (NSString *)getUploadFile_type:(NSString *)type fileType:(NSString *)fileType { NSTimeInterval now = [[NSDate date] timeIntervalSince1970]; NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; - [formatter setDateFormat:@"HHmmss"]; + [formatter setDateFormat:@"yyyyMMddHHmmss"]; NSDate * NowDate = [NSDate dateWithTimeIntervalSince1970:now]; ; NSString * timeStr = [formatter stringFromDate:NowDate]; diff --git a/src/ios/SGRecord/SGRecordOptions.h b/src/ios/SGRecord/SGRecordOptions.h new file mode 100644 index 0000000..0226506 --- /dev/null +++ b/src/ios/SGRecord/SGRecordOptions.h @@ -0,0 +1,29 @@ +// +// Created by 范大德 on 2021/10/9. +// + +#import + +@protocol CDVCommandDelegate; + + +@interface SGRecordOptions : NSObject + + @property (nonatomic, weak) id commandDelegate; + @property (strong) NSString* callbackId; + @property (strong) NSNumber* quality; + @property (assign) UIImagePickerControllerSourceType sourceType; + @property (assign) CGSize targetSize; + @property (assign) BOOL allowsEditing; + @property (assign) BOOL correctOrientation; + @property (assign) BOOL saveToPhotoAlbum; + @property (strong) NSDictionary* popoverOptions; + @property (assign) UIImagePickerControllerCameraDevice cameraDirection; + + @property (assign) BOOL popoverSupported; + @property (assign) BOOL usesGeolocation; + @property (assign) BOOL cropToSize; + + + (instancetype) createFromArguments:(CDVInvokedUrlCommand*)command; +@end + diff --git a/src/ios/SGRecord/SGRecordOptions.m b/src/ios/SGRecord/SGRecordOptions.m new file mode 100644 index 0000000..4652027 --- /dev/null +++ b/src/ios/SGRecord/SGRecordOptions.m @@ -0,0 +1,38 @@ +// +// Created by 范大德 on 2021/10/9. +// + +#import +#import +#import "SGRecordOptions.h" + + + +@implementation SGRecordOptions + ++ (instancetype) createFromArguments:(CDVInvokedUrlCommand*)command +{ + SGRecordOptions* options = [[SGRecordOptions alloc] init]; + options.callbackId = command.callbackId; + options.quality = [command argumentAtIndex:0 withDefault:@(50)]; + options.sourceType = [[command argumentAtIndex:2 withDefault:@(UIImagePickerControllerSourceTypeCamera)] unsignedIntegerValue]; + + NSNumber* targetWidth = [command argumentAtIndex:3 withDefault:nil]; + NSNumber* targetHeight = [command argumentAtIndex:4 withDefault:nil]; + options.targetSize = CGSizeMake(0, 0); + if ((targetWidth != nil) && (targetHeight != nil)) { + options.targetSize = CGSizeMake([targetWidth floatValue], [targetHeight floatValue]); + } + + options.allowsEditing = [[command argumentAtIndex:7 withDefault:@(NO)] boolValue]; + options.correctOrientation = [[command argumentAtIndex:8 withDefault:@(NO)] boolValue]; + options.saveToPhotoAlbum = [[command argumentAtIndex:9 withDefault:@(NO)] boolValue]; + options.popoverOptions = [command argumentAtIndex:10 withDefault:nil]; + options.cameraDirection = [[command argumentAtIndex:11 withDefault:@(UIImagePickerControllerCameraDeviceRear)] unsignedIntegerValue]; + + options.popoverSupported = NO; + options.usesGeolocation = NO; + + return options; +} +@end diff --git a/src/ios/SGRecord/SGRecordViewController.h b/src/ios/SGRecord/SGRecordViewController.h index bb97acd..5494794 100644 --- a/src/ios/SGRecord/SGRecordViewController.h +++ b/src/ios/SGRecord/SGRecordViewController.h @@ -8,6 +8,10 @@ #import -@interface SGRecordViewController : UIViewController +@protocol CDVCommandDelegate; +@class SGRecordOptions; +@interface SGRecordViewController : UIViewController + @property SGRecordOptions* options; + + (instancetype) createFromOption:(SGRecordOptions*)options; @end diff --git a/src/ios/SGRecord/SGRecordViewController.m b/src/ios/SGRecord/SGRecordViewController.m index 09fa7d3..46e8e63 100644 --- a/src/ios/SGRecord/SGRecordViewController.m +++ b/src/ios/SGRecord/SGRecordViewController.m @@ -8,10 +8,14 @@ #import "SGRecordViewController.h" #import +#import +#import #import "SGRecordManager.h" #import "SGRecordSuccessPreview.h" #import "SGRecordProgressView.h" #import "UIButton+Convenience.h" +#import "SGRecordOptions.h" +#import "SGRecordOptions.h" #import "SGMotionManager.h" #define WEAKSELF __weak typeof(self) weakSelf = self; #define STRONGSELF __strong typeof(weakSelf) strongSelf = weakSelf; @@ -30,8 +34,10 @@ @property (nonatomic ,strong) UILabel *tipLabel;// 提示标签 @property (nonatomic ,strong) UIButton *exitButton;// 退出按钮 @property (nonatomic ,strong) UIButton *switchButton;// 切换摄像头按钮 +@property (nonatomic ,strong) UIButton *torchButton;// 切换摄像头按钮 @property (nonatomic ,assign) UIDeviceOrientation lastDeviceOrientation;// 记录屏幕当前方向 -@property (nonatomic ,strong) UILabel *alartLabel;// (提示)拍摄时间太短,不少于3s +@property (nonatomic ,strong) UILabel *alertLabel;// (提示)拍摄时间太短,不少于3s + @end @implementation SGRecordViewController @@ -73,6 +79,8 @@ [self.recordManger shutdown]; } - (void)exitRecordController{ + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + [[self options].commandDelegate sendPluginResult:pluginResult callbackId:self.options.callbackId]; [self dismissViewControllerAnimated:YES completion:nil]; } #if __IPHONE_OS_VERSION_MAX_ALLOWED > __IPHONE_8_4 @@ -81,6 +89,12 @@ } #endif ++ (instancetype) createFromOption:(SGRecordOptions*)options{ + SGRecordViewController* view = [[SGRecordViewController alloc] init]; + view.options = options; + return view; +} + #pragma mark - #pragma mark -拍照 - (void)takephoto{ @@ -127,6 +141,9 @@ // 点击发送 - (void)sendWithImage:(UIImage *)image videoPath:(NSString *)videoPath{ NSLog(@"发送"); + // 加拍照需调整 + CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString: videoPath]; + [[self options].commandDelegate sendPluginResult:pluginResult callbackId:self.options.callbackId]; [self exitRecordController]; } // 点击重拍 @@ -141,6 +158,7 @@ [self showAllOperationViews]; [self.recordManger startUp]; } + #pragma mark - #pragma mark - set、get方法 - (SGRecordManager *)recordManger { @@ -180,22 +198,22 @@ } return _preview; } -- (UILabel *)alartLabel{ - if (!_alartLabel) { - _alartLabel = [[UILabel alloc]init]; - _alartLabel.text = @"拍摄时间太短,不少于3s"; - _alartLabel.font = [UIFont systemFontOfSize:15]; - _alartLabel.textColor = [UIColor whiteColor]; - _alartLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]; - _alartLabel.textAlignment = NSTextAlignmentCenter; - _alartLabel.layer.cornerRadius = 19; - _alartLabel.clipsToBounds = YES; - CGFloat width = [_alartLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 76/2) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.width; - _alartLabel.bounds = CGRectMake(0, 0, width + 30, 76/2); - _alartLabel.center = CGPointMake(self.view.center.x, _tipLabel.center.y - _tipLabel.bounds.size.height/2 - 48/2 - _tipLabel.bounds.size.height/2); - [self.view addSubview:_alartLabel]; +- (UILabel *)alertLabel{ + if (!_alertLabel) { + _alertLabel = [[UILabel alloc]init]; + _alertLabel.text = @"拍摄时间太短,不少于3s"; + _alertLabel.font = [UIFont systemFontOfSize:15]; + _alertLabel.textColor = [UIColor whiteColor]; + _alertLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6]; + _alertLabel.textAlignment = NSTextAlignmentCenter; + _alertLabel.layer.cornerRadius = 19; + _alertLabel.clipsToBounds = YES; + CGFloat width = [_alertLabel.text boundingRectWithSize:CGSizeMake(MAXFLOAT, 76/2) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.width; + _alertLabel.bounds = CGRectMake(0, 0, width + 30, 76/2); + _alertLabel.center = CGPointMake(self.view.center.x, _tipLabel.center.y - _tipLabel.bounds.size.height/2 - 48/2 - _tipLabel.bounds.size.height/2); + [self.view addSubview:_alertLabel]; } - return _alartLabel; + return _alertLabel; } #pragma mark - #pragma mark -Set Up UI @@ -219,7 +237,9 @@ UILabel *tipLabel = [[UILabel alloc]init]; tipLabel.bounds = CGRectMake(0, 0, 200, 20); tipLabel.center = CGPointMake(self.view.center.x, self.view.bounds.size.height - 160 - 13/2); - tipLabel.text = @"点击拍照,长按拍摄"; + // 拍照相关 + // tipLabel.text = @"点击拍照,长按拍摄"; + tipLabel.text = @""; tipLabel.font = [UIFont systemFontOfSize:13]; tipLabel.textAlignment = NSTextAlignmentCenter; tipLabel.textColor = [UIColor whiteColor]; @@ -232,6 +252,15 @@ [self.view addSubview:switchButton]; _switchButton = switchButton; + // 闪光灯按钮 + AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; + if ([device hasTorch] && [device hasFlash]) { + UIButton *torchButton = [UIButton image:@"btn_video_flash_open"target:self action:@selector(torchButtonPressed:)]; + torchButton.frame = CGRectMake(self.view.bounds.size.width - (44 - 5) * 3 , 10, 44, 44); + [self.view addSubview:torchButton]; + _torchButton = torchButton; + } + // 对焦手势 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)]; tapGesture.delegate = self; @@ -269,6 +298,24 @@ button.selected = !button.selected; [self.recordManger changeCameraInputDeviceisFront:button.selected]; } + +// 闪光灯 +- (void)torchButtonPressed:(id)sender +{ + AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; + NSError *error; + BOOL b = [device lockForConfiguration:&error]; + if (!b) { + if (error) { + NSLog(@"lock torch configuration error:%@", error.localizedDescription); + } + return; + } + device.torchMode = + (device.torchMode == AVCaptureTorchModeOff ? AVCaptureTorchModeOn : AVCaptureTorchModeOff); + [device unlockForConfiguration]; +} + #pragma mark - #pragma mark -显示或隐藏界面 // 显示所有操作按钮 @@ -278,6 +325,7 @@ [self.exitButton setHidden:NO]; [self.tipLabel setHidden:NO]; [self.switchButton setHidden:NO]; + [self.torchButton setHidden:NO]; }); } // 隐藏所有操作按钮 @@ -287,6 +335,7 @@ [self.exitButton setHidden:YES]; [self.tipLabel setHidden:YES]; [self.switchButton setHidden:YES]; + [self.torchButton setHidden:YES]; }); } // 拍摄结束后显示退出按钮和切换摄像头按钮 @@ -294,6 +343,7 @@ dispatch_async(dispatch_get_main_queue(), ^{ [self.exitButton setHidden:NO]; [self.switchButton setHidden:NO]; + [self.torchButton setHidden:NO]; }); } // 开始拍摄时隐藏退出和切换摄像头按钮 @@ -301,6 +351,7 @@ dispatch_async(dispatch_get_main_queue(), ^{ [self.exitButton setHidden:YES]; [self.switchButton setHidden:YES]; + [self.torchButton setHidden:YES]; }); } #pragma mark - @@ -333,14 +384,18 @@ // 录制时间太短 NSLog(@"录制时间太短"); [self stopRecord:NO]; - [self alart];//提示用户 + [self alert];//提示用户 [self.recordButton resetScale]; } else if (_timeInterval < RECORD_TIME) { - // 拍照 - NSLog(@"拍照"); - [self.recordButton setEnabled:NO]; - [self hideAllOperationViews]; - [self takephoto]; + // 拍照相关 +// NSLog(@"拍照"); +// [self.recordButton setEnabled:NO]; +// [self hideAllOperationViews]; +// [self takephoto]; + NSLog(@"录制时间太短"); + [self stopRecord:NO]; + [self alert];//提示用户 + [self.recordButton resetScale]; } else { // 拍摄视频 NSLog(@"结束录制"); @@ -438,15 +493,15 @@ _lastDeviceOrientation = deviceOrientation; NSLog(@"deviceOrientation:%ld",(long)deviceOrientation); } -- (void)alart{ - [self.view bringSubviewToFront:self.alartLabel]; +- (void)alert{ + [self.view bringSubviewToFront:self.alertLabel]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - if (_alartLabel) { + if (_alertLabel) { [UIView animateWithDuration:0.25 animations:^{ - _alartLabel.alpha = 0; + _alertLabel.alpha = 0; } completion:^(BOOL finished) { - [_alartLabel removeFromSuperview]; - _alartLabel = nil; + [_alertLabel removeFromSuperview]; + _alertLabel = nil; }]; } });