BasicModeViewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //
  2. // BasicModeViewController.m
  3. // RA Image
  4. //
  5. // Created by Jack on 2017/5/3.
  6. // Copyright © 2017年 USAI. All rights reserved.
  7. //
  8. #import "BasicModeViewController.h"
  9. #import "ScannerViewController.h"
  10. #import "PhotoListViewController.h"
  11. @interface BasicModeViewController ()<UITextFieldDelegate>
  12. @end
  13. @implementation BasicModeViewController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. // Do any additional setup after loading the view.
  17. }
  18. - (void)didReceiveMemoryWarning {
  19. [super didReceiveMemoryWarning];
  20. // Dispose of any resources that can be recreated.
  21. }
  22. - (void)viewWillAppear:(BOOL)animated {
  23. [super viewWillAppear:animated];
  24. [self loadSavedPhotoCount];
  25. }
  26. - (void)showPhotoList {
  27. if (!self.photos.count) {
  28. [RAUtils message_alert:@"There is no photo" title:@"Note" controller:self];
  29. return;
  30. }
  31. PhotoListViewController *photoListVC = (PhotoListViewController *)[self viewControllerInStoryboard:@"PhotoList" withId:@"PhotoListViewController"];
  32. photoListVC.photos = [self.photos mutableCopy];
  33. [self.navigationController pushViewController:photoListVC animated:YES];
  34. }
  35. - (void)showScanner {
  36. __weak typeof(self) weakself = self;
  37. ScannerViewController *scannerVC = [[UIStoryboard storyboardWithName:@"cam_scan" bundle:nil] instantiateViewControllerWithIdentifier:@"NewScannerViewController"];
  38. scannerVC.returnCode = ^(NSString *code) {
  39. // 扫描成功保存扫描值
  40. if (code.length) {
  41. if (weakself) {
  42. __strong typeof(weakself) strongself = weakself;
  43. strongself.barcode = code;
  44. }
  45. }
  46. };
  47. [self presentViewController:scannerVC animated:YES completion:nil];
  48. }
  49. - (void)showCamera {
  50. UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
  51. imgPicker.delegate = self;
  52. // AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  53. // if (status != AVAuthorizationStatusAuthorized) {
  54. // // 未授权
  55. // [RAUtils message_alert:@"please allow app use camera" title:@"Warning" controller:weakself];
  56. // } else {
  57. imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  58. imgPicker.allowsEditing = YES;
  59. [self presentViewController:imgPicker animated:YES completion:nil];
  60. // }
  61. }
  62. - (void)showPhotoLibrary {
  63. UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
  64. imgPicker.delegate = self;
  65. // ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
  66. // if (author != ALAuthorizationStatusAuthorized) {
  67. // [RAUtils message_alert:@"please allow app visit photo library" title:@"Warning" controller:weakself];
  68. // } else {
  69. imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  70. imgPicker.allowsEditing = NO;
  71. [self presentViewController:imgPicker animated:YES completion:nil];
  72. // }
  73. }
  74. - (void)showBarcodeInput {
  75. __weak typeof(self) weakself = self;
  76. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Please enter Barcode" message:nil preferredStyle:UIAlertControllerStyleAlert];
  77. [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  78. textField.delegate = weakself;
  79. }];
  80. UIAlertAction *OK = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  81. }];
  82. UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  83. }];
  84. [alert addAction:cancel];
  85. [alert addAction:OK];
  86. [self presentViewController:alert animated:YES completion:nil];
  87. }
  88. - (void)receiveImage:(UIImage *)img {
  89. }
  90. - (void)saveImage:(UIImage *)img {
  91. NSString *dir = [NSString stringWithFormat:@"%@/%@",[RAUtils appCacheDirectory],self.name];
  92. NSDate * date = [NSDate date];
  93. NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
  94. [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
  95. NSString * name = [[dateFormatter stringFromDate:date] stringByAppendingPathExtension:@"png"];
  96. NSString *path = [dir stringByAppendingPathComponent:name];
  97. [RAUtils saveData:UIImagePNGRepresentation(img) toPath:path];
  98. }
  99. - (void)loadSavedPhotoCount {
  100. NSString *dir = [NSString stringWithFormat:@"%@/%@",[RAUtils appCacheDirectory],self.name];
  101. NSFileManager *manager = [NSFileManager defaultManager];
  102. NSArray *files = [manager contentsOfDirectoryAtPath:dir error:nil];
  103. if (files.count) {
  104. NSMutableArray *photos = [NSMutableArray array];
  105. for (NSString *name in files) {
  106. if ([name hasSuffix:@".png"]) {
  107. NSString *path = [dir stringByAppendingPathComponent:name];
  108. UIImage *img = [UIImage imageWithContentsOfFile:path];
  109. NSDictionary *photoDic = @{
  110. @"photo" : img,
  111. @"check" : @(NO),
  112. @"path" : path
  113. };
  114. [photos addObject:photoDic];
  115. }
  116. }
  117. self.photoCount = photos.count;
  118. self.photos = photos;
  119. } else {
  120. self.photoCount = 0;
  121. self.photos = nil;
  122. }
  123. }
  124. #pragma mark - Public Method
  125. - (void)clickCameraButton {
  126. // __weak typeof(self) weakself = self;
  127. //
  128. //
  129. // UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  130. // UIAlertAction *library = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  131. // [weakself showCamera];
  132. // }];
  133. //
  134. // UIAlertAction *camera = [UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  135. // [weakself showPhotoLibrary];
  136. // }];
  137. //
  138. // UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  139. //
  140. // }];
  141. //
  142. // [aler addAction:library];
  143. // [aler addAction:camera];
  144. // [aler addAction:cancel];
  145. //
  146. // [self presentViewController:aler animated:YES completion:nil];
  147. [self showCamera];
  148. }
  149. #pragma mark - Image Picker Delegate
  150. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
  151. {
  152. UIImage *image = nil;
  153. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  154. image = [info objectForKey:UIImagePickerControllerEditedImage];
  155. [picker dismissViewControllerAnimated:YES completion:nil];
  156. } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
  157. image = [info objectForKey:UIImagePickerControllerOriginalImage];
  158. [picker dismissViewControllerAnimated:YES completion:nil];
  159. }
  160. [self saveImage:image];
  161. [self receiveImage:image];
  162. }
  163. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  164. [picker dismissViewControllerAnimated:YES completion:nil];
  165. }
  166. #pragma mark - TextField Delegate
  167. - (void)textFieldDidEndEditing:(UITextField *)textField {
  168. self.barcode = textField.text;
  169. }
  170. @end