BasicModeViewController.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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. #import "UploadViewController.h"
  12. #import <AVFoundation/AVFoundation.h>
  13. #import <AssetsLibrary/AssetsLibrary.h>
  14. #import "TakePhotoPreviewController.h"
  15. #import "ImageUtils.h"
  16. #pragma clang diagnostic push
  17. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  18. @interface BasicModeViewController ()<UITextFieldDelegate>
  19. @property (nonatomic,strong) UIImagePickerController *imgPicker;
  20. @end
  21. @implementation BasicModeViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. // Do any additional setup after loading the view.
  25. self.photoCount = 0;
  26. UIBarButtonItem *uploadListItem = [[UIBarButtonItem alloc] initWithTitle:@"Upload List" style:UIBarButtonItemStylePlain target:self action:@selector(showUploadList)];
  27. self.navigationItem.rightBarButtonItem = uploadListItem;
  28. AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  29. [appdelegate.uploadManager addObserver:self
  30. forKeyPath:@"queue_status"
  31. options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld)
  32. context:@"queue_status changed"];
  33. if (appdelegate.compressFile) {
  34. [RAUtils message_alert:@"The photos will be compressed" title:@"Attention" controller:self];
  35. }
  36. self.title = self.name;
  37. }
  38. -(void) dealloc
  39. {
  40. AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  41. [appdelegate.uploadManager removeObserver:self forKeyPath:@"queue_status"];
  42. }
  43. - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
  44. {
  45. if([keyPath isEqualToString:@"queue_status"])
  46. {
  47. NSString* msg=@"";
  48. BOOL useToast = NO;
  49. switch ([[change objectForKey:NSKeyValueChangeNewKey] intValue]) {
  50. case QueueStatusFinish: {
  51. msg=@"Upload is complete.";
  52. [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
  53. useToast = NO;
  54. }
  55. break;
  56. case QueueStatusAdd: {
  57. msg=@"New tasks added.";
  58. [[UIApplication sharedApplication] setIdleTimerDisabled:YES];
  59. useToast = YES;
  60. }
  61. break;
  62. case QueueStatusFinishWithError:
  63. {
  64. NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  65. NSDictionary* dict=[userDefaults objectForKey:@"UploadSettingKey"];
  66. if([dict[@"auto_rm_error"] boolValue])
  67. msg=@"Upload is complete.";
  68. else
  69. msg=@"Some of the tasks have not been successfully uploaded.\n Check upload list for detail.";
  70. [[UIApplication sharedApplication] setIdleTimerDisabled:NO];
  71. useToast = NO;
  72. }
  73. break;
  74. default:
  75. break;
  76. }
  77. __weak typeof(self) weakself = self;
  78. dispatch_async(dispatch_get_main_queue(), ^{
  79. if(msg.length>0) {
  80. if (useToast) {
  81. [weakself.view makeToast:msg duration:3.0 position:CSToastPositionCenter];
  82. } else {
  83. UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:msg preferredStyle:UIAlertControllerStyleAlert];
  84. UIAlertAction *action = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  85. }];
  86. [alert addAction:action];
  87. [weakself presentViewController:alert animated:YES completion:nil];
  88. }
  89. }
  90. });
  91. }
  92. }
  93. - (void)didReceiveMemoryWarning {
  94. [super didReceiveMemoryWarning];
  95. // Dispose of any resources that can be recreated.
  96. }
  97. - (void)viewWillAppear:(BOOL)animated {
  98. [super viewWillAppear:animated];
  99. }
  100. - (NSMutableArray *)photos {
  101. if (!_photos) {
  102. _photos = [NSMutableArray array];
  103. }
  104. return _photos;
  105. }
  106. - (void)showUploadList {
  107. UploadViewController *upVC = (UploadViewController *)[self viewControllerInStoryboard:@"Upload" withId:@"UploadViewController"];
  108. [self.navigationController pushViewController:upVC animated:YES];
  109. }
  110. - (void)showPhotoList {
  111. if (!self.photos.count) {
  112. // [RAUtils message_alert:@"There is no photo" title:@"Note" controller:self];
  113. [self showCamera];
  114. return;
  115. }
  116. PhotoListViewController *photoListVC = (PhotoListViewController *)[self viewControllerInStoryboard:@"PhotoList" withId:@"PhotoListViewController"];
  117. // photoListVC.photos = self.photos;
  118. photoListVC.modeVC = self;
  119. [self.navigationController pushViewController:photoListVC animated:YES];
  120. }
  121. - (void)showScanner {
  122. __weak typeof(self) weakself = self;
  123. ScannerViewController *scannerVC = [[UIStoryboard storyboardWithName:@"cam_scan" bundle:nil] instantiateViewControllerWithIdentifier:@"NewScannerViewController"];
  124. scannerVC.returnCode = ^(NSString *code) {
  125. // 扫描成功保存扫描值
  126. if (code.length) {
  127. if (weakself) {
  128. __strong typeof(weakself) strongself = weakself;
  129. strongself.barcode = code;
  130. }
  131. }
  132. };
  133. [self presentViewController:scannerVC animated:YES completion:nil];
  134. }
  135. - (void)showCamera {
  136. if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  137. [RAUtils message_alert:@"Camera is not available" title:@"Warning" controller:self];
  138. return;
  139. }
  140. //相机权限
  141. AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
  142. if (authStatus ==AVAuthorizationStatusRestricted ||//此应用程序没有被授权访问的照片数据。可能是家长控制权限
  143. authStatus ==AVAuthorizationStatusDenied) //用户已经明确否认了这一照片数据的应用程序访问
  144. {
  145. // 无权限 引导去开启
  146. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  147. if ([[UIApplication sharedApplication]canOpenURL:url]) {
  148. [[UIApplication sharedApplication]openURL:url];// 打开权限后返回应用会重启
  149. } else {
  150. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  151. NSString *appName = [infoDict objectForKey:@"CFBundleName"];
  152. [RAUtils message_alert:[NSString stringWithFormat:@"%@ need Authorization to use Camera",appName] title:@"Warning" controller:self];
  153. }
  154. return;
  155. }
  156. // UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
  157. // imgPicker.delegate = self;
  158. //
  159. // imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  160. // imgPicker.allowsEditing = YES;
  161. // [self presentViewController:imgPicker animated:YES completion:nil];
  162. CGFloat w = CGRectGetWidth(self.view.frame);
  163. CGFloat h = CGRectGetHeight(self.view.frame);
  164. if (self.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || self.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
  165. // 修复横屏时打开相机Control位置不正确
  166. CGFloat tmp = w;
  167. w = h;
  168. h = tmp;
  169. }
  170. // 自定义相机控制视图
  171. UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
  172. imgPicker.delegate = self;
  173. imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  174. imgPicker.allowsEditing = NO;
  175. imgPicker.showsCameraControls = NO;
  176. UIView* overlay = [[UIView alloc]initWithFrame:CGRectMake(0, h - 150 , w, 150)];
  177. // Cancel
  178. UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  179. cancelBtn.frame = CGRectMake(20, (150 - 40) * 0.5, 60, 40);
  180. [cancelBtn setTitle:@"cancel" forState:UIControlStateNormal];
  181. [cancelBtn addTarget:self action:@selector(cancelCameraClick:) forControlEvents:UIControlEventTouchUpInside];
  182. [overlay addSubview:cancelBtn];
  183. // Take
  184. UIButton *takeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  185. takeBtn.frame = CGRectMake((w - 60) * 0.5, (150 - 60) * 0.5, 60, 60);
  186. // [takeBtn setTitle:@"take" forState:UIControlStateNormal];
  187. [takeBtn setImage:[UIImage imageNamed:@"take_photo"] forState:UIControlStateNormal];
  188. [takeBtn addTarget:self action:@selector(takePhotoClick:) forControlEvents:UIControlEventTouchUpInside];
  189. [overlay addSubview:takeBtn];
  190. // flash
  191. UIButton *flashBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  192. flashBtn.frame = CGRectMake(w - 80, (150 - 60) * 0.5, 60, 60);
  193. [flashBtn setImage:[UIImage imageNamed:@"flash_auto"] forState:UIControlStateNormal];
  194. [flashBtn addTarget:self action:@selector(flashBtnClick:) forControlEvents:UIControlEventTouchUpInside];
  195. [overlay addSubview:flashBtn];
  196. // // camera
  197. // UIButton *cameraChangeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  198. // cameraChangeBtn.frame = CGRectMake(220, 10, 60, 40);
  199. // [cameraChangeBtn setTitle:@"change" forState:UIControlStateNormal];
  200. // [cameraChangeBtn addTarget:self action:@selector(cameraChangeClick:) forControlEvents:UIControlEventTouchUpInside];
  201. // [overlay addSubview:cameraChangeBtn];
  202. // 相机属性设置得在设置SourceType后设置
  203. imgPicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
  204. //把自定义的view设置到imagepickercontroller的overlay属性中
  205. imgPicker.cameraOverlayView = overlay;
  206. self.imgPicker = imgPicker;
  207. [self presentViewController:imgPicker animated:YES completion:nil];
  208. }
  209. - (void)showPhotoLibrary {
  210. if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
  211. [RAUtils message_alert:@"Photo Library is not available" title:@"Warning" controller:self];
  212. return;
  213. }
  214. //相册权限
  215. ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
  216. if (author ==ALAuthorizationStatusRestricted || author ==ALAuthorizationStatusDenied){
  217. //无权限 引导去开启
  218. NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
  219. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  220. [[UIApplication sharedApplication] openURL:url];
  221. } else {
  222. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  223. NSString *appName = [infoDict objectForKey:@"CFBundleName"];
  224. [RAUtils message_alert:[NSString stringWithFormat:@"%@ need Authorization to use photo library",appName] title:@"Warning" controller:self];
  225. }
  226. return;
  227. }
  228. UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
  229. imgPicker.delegate = self;
  230. imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  231. imgPicker.allowsEditing = NO;
  232. [self presentViewController:imgPicker animated:YES completion:nil];
  233. }
  234. - (void)showBarcodeInput:(UIKeyboardType)keyboardType {
  235. [self tapResignFirstResponder:nil];
  236. __weak typeof(self) weakself = self;
  237. UIAlertController *alert = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"Please enter %@ Code",self.barcodeTitle] message:nil preferredStyle:UIAlertControllerStyleAlert];
  238. [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  239. textField.text = weakself.barcode;
  240. textField.delegate = weakself;
  241. textField.clearButtonMode = UITextFieldViewModeWhileEditing;
  242. textField.keyboardType = keyboardType;
  243. }];
  244. UIAlertAction *OK = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  245. }];
  246. [alert addAction:OK];
  247. [self presentViewController:alert animated:YES completion:nil];
  248. }
  249. - (void)showTopImage {
  250. NSDictionary *item = [self.photos firstObject];
  251. UIImage *img = [UIImage imageWithContentsOfFile:[item objectForKey:@"path"]];
  252. [self receiveImage:img];
  253. }
  254. - (NSString *)saveImage:(UIImage *)img {
  255. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  256. if (appDelegate.compressFile) {
  257. img = [ImageUtils img_compress:img kbsize:1024];
  258. }
  259. NSString *dir = [NSString stringWithFormat:@"%@/%@",NSTemporaryDirectory(),self.name];
  260. NSDate * date = [NSDate date];
  261. NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
  262. [dateFormatter setDateFormat:@"YYYYMMdd_HHmmssSS"];
  263. NSString * name = [[dateFormatter stringFromDate:date] stringByAppendingPathExtension:@"png"];
  264. name=[self.barcodeTitle stringByAppendingString:name];
  265. // NSString *name = [[NSUUID UUID] UUIDString];
  266. // name = [name stringByAppendingPathExtension:@"png"];
  267. NSString *path = [dir stringByAppendingPathComponent:name];
  268. if ([RAUtils saveData:UIImagePNGRepresentation(img) toPath:path]) {
  269. return path;
  270. }
  271. return nil;
  272. }
  273. //- (void)loadSavedPhotoCount {
  274. // NSString *dir = [NSString stringWithFormat:@"%@/%@",[RAUtils appCacheDirectory],self.name];
  275. // NSFileManager *manager = [NSFileManager defaultManager];
  276. // NSArray *files = [manager contentsOfDirectoryAtPath:dir error:nil];
  277. // if (files.count) {
  278. // NSMutableArray *photos = [NSMutableArray array];
  279. // for (NSString *name in files) {
  280. // if ([name hasSuffix:@".png"]) {
  281. // NSString *path = [dir stringByAppendingPathComponent:name];
  282. // UIImage *img = [UIImage imageWithContentsOfFile:path];
  283. // NSString *md5 = [RAUtils md5WithFile:path];
  284. // NSDictionary *photoDic = @{
  285. // @"photo" : img,
  286. // @"check" : @(NO),
  287. // @"path" : path,
  288. // @"md5" : md5
  289. // };
  290. // [photos addObject:photoDic];
  291. // }
  292. // }
  293. // self.photoCount = photos.count;
  294. // self.photos = photos;
  295. // } else {
  296. // self.photoCount = 0;
  297. // self.photos = nil;
  298. // }
  299. //}
  300. - (void)clearPhotos {
  301. [self.photos removeAllObjects];
  302. self.photoCount = 0;
  303. }
  304. #pragma mark - Public Method
  305. - (void)clickCameraButton {
  306. // __weak typeof(self) weakself = self;
  307. //
  308. //
  309. // UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  310. // UIAlertAction *library = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  311. // [weakself showCamera];
  312. // }];
  313. //
  314. // UIAlertAction *camera = [UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  315. // [weakself showPhotoLibrary];
  316. // }];
  317. //
  318. // UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  319. //
  320. // }];
  321. //
  322. // [aler addAction:library];
  323. // [aler addAction:camera];
  324. // [aler addAction:cancel];
  325. //
  326. // [self presentViewController:aler animated:YES completion:nil];
  327. [self showCamera];
  328. }
  329. - (void)moveTmpImageToCache:(NSString *)img {
  330. NSString *dir = [NSString stringWithFormat:@"%@/%@",NSTemporaryDirectory(),self.name];
  331. NSString *tmpPath = [dir stringByAppendingPathComponent:img];
  332. NSString *cachePath = [NSString stringWithFormat:@"%@/%@/%@",[RAUtils appCacheDirectory],self.name,img];
  333. NSFileManager *fileManager = [NSFileManager defaultManager];
  334. // 保证移动目标文件夹存在
  335. [fileManager createDirectoryAtPath:[cachePath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
  336. NSError *err;
  337. BOOL res = [fileManager moveItemAtPath:tmpPath toPath:cachePath error:&err];
  338. if (res) {
  339. DebugLog(@"move tmp to cache");
  340. } else {
  341. DebugLog(@"move tmp to cache failed %@",err);
  342. }
  343. }
  344. - (BOOL)shouldUploadWithCurrentNerworkStatus {
  345. AppDelegate* appdelegate=(AppDelegate*)[UIApplication sharedApplication].delegate;
  346. if (appdelegate.uploadManager.onlyWiFi && appdelegate.uploadManager.reach.currentReachabilityStatus != ReachableViaWiFi) {
  347. return NO;
  348. }
  349. return YES;
  350. }
  351. #pragma mark - Image Picker Button Action
  352. - (void)cancelCameraClick:(UIButton *)sender {
  353. [self.imgPicker dismissViewControllerAnimated:YES completion:nil];
  354. }
  355. - (void)takePhotoClick:(UIButton *)sender {
  356. [self.imgPicker takePicture];
  357. }
  358. - (void)flashBtnClick:(UIButton *)sender {
  359. UIImagePickerControllerCameraDevice device = self.imgPicker.cameraDevice;
  360. UIImagePickerControllerCameraFlashMode flashMode = self.imgPicker.cameraFlashMode;
  361. NSString *flashMordeString = @"flash_auto";
  362. UIImage *flash_img = [UIImage imageNamed:flashMordeString];
  363. switch (flashMode) {
  364. case UIImagePickerControllerCameraFlashModeAuto: {
  365. flashMode = UIImagePickerControllerCameraFlashModeOn;
  366. flashMordeString = @"flash_on";
  367. flash_img = [UIImage imageNamed:flashMordeString];
  368. }
  369. break;
  370. case UIImagePickerControllerCameraFlashModeOn: {
  371. flashMode = UIImagePickerControllerCameraFlashModeOff;
  372. flashMordeString = @"flash_off";
  373. flash_img = [UIImage imageNamed:flashMordeString];
  374. }
  375. break;
  376. case UIImagePickerControllerCameraFlashModeOff: {
  377. flashMode = UIImagePickerControllerCameraFlashModeAuto;
  378. flashMordeString = @"flash_auto";
  379. flash_img = [UIImage imageNamed:flashMordeString];
  380. }
  381. break;
  382. default:
  383. break;
  384. }
  385. if ([UIImagePickerController isFlashAvailableForCameraDevice:device]) {
  386. self.imgPicker.cameraFlashMode = flashMode;
  387. [sender setImage:[UIImage imageNamed:flashMordeString] forState:UIControlStateNormal];
  388. }
  389. }
  390. - (void)cameraChangeClick:(UIButton *)sender {
  391. UIImagePickerControllerCameraDevice device = self.imgPicker.cameraDevice;
  392. if (device == UIImagePickerControllerCameraDeviceRear) {
  393. device = UIImagePickerControllerCameraDeviceFront;
  394. } else {
  395. device = UIImagePickerControllerCameraDeviceRear;
  396. }
  397. if ([UIImagePickerController isCameraDeviceAvailable:device]) {
  398. self.imgPicker.cameraDevice = device;
  399. }
  400. }
  401. #pragma mark - Image Picker Delegate
  402. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
  403. {
  404. UIImage *image = nil;
  405. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  406. if (self.imgPicker.viewControllers.count > 1) return;// 避免连续多次拍照,后多次present
  407. image = [info objectForKey:UIImagePickerControllerOriginalImage];
  408. // [picker dismissViewControllerAnimated:YES completion:nil];
  409. __weak typeof(self) weakself = self;
  410. TakePhotoPreviewController *preVC = (TakePhotoPreviewController *)[self viewControllerInStoryboard:@"Mode" withId:@"TakePhotoPreviewController"];
  411. __weak typeof(preVC) weakPreVC = preVC;
  412. preVC.photoHandler = ^(UIImage *img){
  413. if (img) {
  414. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  415. NSString *path = [weakself saveImage:img];
  416. dispatch_async(dispatch_get_main_queue(), ^{
  417. if (path) {// 保存成功
  418. [weakself receiveImage:img];
  419. NSString *md5 = [RAUtils md5WithFile:path];
  420. NSMutableDictionary *photoDic = @{
  421. @"check" : @(NO),
  422. @"file" : [path lastPathComponent],
  423. @"md5" : md5,
  424. @"path" : path,
  425. }.mutableCopy;
  426. [weakself.photos insertObject:photoDic atIndex:0];
  427. weakself.photoCount++;
  428. } else {
  429. [RAUtils message_alert:@"Save photo failed, storage full?" title:@"Warning" controller:weakPreVC];
  430. }
  431. });
  432. });
  433. }
  434. };
  435. preVC.preImage = image;
  436. [self.imgPicker pushViewController:preVC animated:YES];
  437. } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
  438. image = [info objectForKey:UIImagePickerControllerOriginalImage];
  439. [picker dismissViewControllerAnimated:YES completion:nil];
  440. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  441. NSString *path = [self saveImage:image];
  442. dispatch_async(dispatch_get_main_queue(), ^{
  443. if (path) {// 保存成功
  444. [self receiveImage:image];
  445. NSString *md5 = [RAUtils md5WithFile:path];
  446. NSMutableDictionary *photoDic = @{
  447. @"check" : @(NO),
  448. @"file" : [path lastPathComponent],
  449. @"md5" : md5,
  450. @"path" : path,
  451. }.mutableCopy;
  452. [self.photos insertObject:photoDic atIndex:0];
  453. self.photoCount++;
  454. } else {
  455. [RAUtils message_alert:@"Save photo failed, storage full?" title:@"Warning" controller:self];
  456. }
  457. });
  458. });
  459. }
  460. }
  461. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  462. [picker dismissViewControllerAnimated:YES completion:nil];
  463. }
  464. #pragma mark - TextField Delegate
  465. - (void)textFieldDidEndEditing:(UITextField *)textField {
  466. self.barcode = textField.text;
  467. }
  468. #pragma mark - SubClass Override
  469. - (void)receiveImage:(UIImage *)img {
  470. }
  471. -(void) verify:(NSMutableDictionary*) params
  472. {
  473. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  474. // appDelegate.address = self.address;
  475. __weak typeof(self) weakself = self;
  476. NSString* title=[@"Verify " stringByAppendingString:self.barcodeTitle];
  477. UIAlertView *alert = [RAUtils waiting_alert:@"Please wait" title:title];
  478. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  479. // if (weakself) {
  480. // __strong typeof(weakself) strongself = weakself;
  481. NSDictionary *result_json = [RAINetwork Verify:params];
  482. dispatch_async(dispatch_get_main_queue(), ^{
  483. [alert dismissWithClickedButtonIndex:0 animated:YES];
  484. int result = [[result_json objectForKey:@"result"] intValue];
  485. // BOOL rememberLogin = [[strongself userDefaultsValue:kRememberLogin] boolValue];
  486. // [self addTasks];
  487. // return;
  488. switch (result) {
  489. case 0:
  490. {
  491. //code not exist;
  492. [RAUtils message_alert:weakself.verify_msg0 title:@"Warring" controller:weakself];
  493. // [weakself.view makeToast:weakself.verify_msg0 duration:3.0 position:CSToastPositionCenter];
  494. break;
  495. }
  496. case 1:
  497. {
  498. [RAUtils message_alert:@"Model/Manufacturer does not match" title:@"Warring" controller:weakself];
  499. // [weakself.view makeToast:@"Model/Manufacturer does not match" duration:3.0 position:CSToastPositionCenter];
  500. break;
  501. }
  502. case 2:
  503. {
  504. //upload;
  505. [self addTasks];
  506. break;
  507. }
  508. case RESULT_NET_ERROR:
  509. {
  510. [RAUtils message_alert:@"Can not connect to server" title:title controller:weakself];
  511. }
  512. default:
  513. break;
  514. }
  515. });
  516. // }
  517. });
  518. }
  519. @end
  520. #pragma clang diagnostic pop