| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- //
- // BasicModeViewController.m
- // RA Image
- //
- // Created by Jack on 2017/5/3.
- // Copyright © 2017年 USAI. All rights reserved.
- //
- #import "BasicModeViewController.h"
- #import "ScannerViewController.h"
- #import "PhotoListViewController.h"
- @interface BasicModeViewController ()<UITextFieldDelegate>
- @end
- @implementation BasicModeViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self loadSavedPhotoCount];
- }
- - (void)showPhotoList {
- if (!self.photos.count) {
- [RAUtils message_alert:@"There is no photo" title:@"Note" controller:self];
- return;
- }
- PhotoListViewController *photoListVC = (PhotoListViewController *)[self viewControllerInStoryboard:@"PhotoList" withId:@"PhotoListViewController"];
- photoListVC.photos = [self.photos mutableCopy];
- [self.navigationController pushViewController:photoListVC animated:YES];
- }
- - (void)showScanner {
- __weak typeof(self) weakself = self;
- ScannerViewController *scannerVC = [[UIStoryboard storyboardWithName:@"cam_scan" bundle:nil] instantiateViewControllerWithIdentifier:@"NewScannerViewController"];
- scannerVC.returnCode = ^(NSString *code) {
- // 扫描成功保存扫描值
- if (code.length) {
-
- if (weakself) {
- __strong typeof(weakself) strongself = weakself;
-
- strongself.barcode = code;
- }
-
- }
- };
- [self presentViewController:scannerVC animated:YES completion:nil];
- }
- - (void)showCamera {
- UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
- imgPicker.delegate = self;
- // AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
- // if (status != AVAuthorizationStatusAuthorized) {
- // // 未授权
- // [RAUtils message_alert:@"please allow app use camera" title:@"Warning" controller:weakself];
- // } else {
- imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
- imgPicker.allowsEditing = YES;
- [self presentViewController:imgPicker animated:YES completion:nil];
-
- // }
- }
- - (void)showPhotoLibrary {
-
- UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
- imgPicker.delegate = self;
- // ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
- // if (author != ALAuthorizationStatusAuthorized) {
- // [RAUtils message_alert:@"please allow app visit photo library" title:@"Warning" controller:weakself];
- // } else {
- imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- imgPicker.allowsEditing = NO;
- [self presentViewController:imgPicker animated:YES completion:nil];
- // }
- }
- - (void)showBarcodeInput {
- __weak typeof(self) weakself = self;
- UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Please enter Barcode" message:nil preferredStyle:UIAlertControllerStyleAlert];
- [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
-
- textField.delegate = weakself;
-
- }];
-
- UIAlertAction *OK = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- }];
-
- UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
-
- }];
-
- [alert addAction:cancel];
- [alert addAction:OK];
-
- [self presentViewController:alert animated:YES completion:nil];
-
- }
- - (void)receiveImage:(UIImage *)img {
-
- }
- - (void)saveImage:(UIImage *)img {
- NSString *dir = [NSString stringWithFormat:@"%@/%@",[RAUtils appCacheDirectory],self.name];
-
- NSDate * date = [NSDate date];
- NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
- NSString * name = [[dateFormatter stringFromDate:date] stringByAppendingPathExtension:@"png"];
- NSString *path = [dir stringByAppendingPathComponent:name];
-
- [RAUtils saveData:UIImagePNGRepresentation(img) toPath:path];
- }
- - (void)loadSavedPhotoCount {
- NSString *dir = [NSString stringWithFormat:@"%@/%@",[RAUtils appCacheDirectory],self.name];
- NSFileManager *manager = [NSFileManager defaultManager];
- NSArray *files = [manager contentsOfDirectoryAtPath:dir error:nil];
- if (files.count) {
- NSMutableArray *photos = [NSMutableArray array];
- for (NSString *name in files) {
- if ([name hasSuffix:@".png"]) {
- NSString *path = [dir stringByAppendingPathComponent:name];
- UIImage *img = [UIImage imageWithContentsOfFile:path];
- NSDictionary *photoDic = @{
- @"photo" : img,
- @"check" : @(NO),
- @"path" : path
- };
- [photos addObject:photoDic];
- }
- }
- self.photoCount = photos.count;
- self.photos = photos;
- } else {
- self.photoCount = 0;
- self.photos = nil;
- }
- }
- #pragma mark - Public Method
- - (void)clickCameraButton {
- // __weak typeof(self) weakself = self;
- //
- //
- // UIAlertController *aler = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- // UIAlertAction *library = [UIAlertAction actionWithTitle:@"Camera" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- // [weakself showCamera];
- // }];
- //
- // UIAlertAction *camera = [UIAlertAction actionWithTitle:@"Photo Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- // [weakself showPhotoLibrary];
- // }];
- //
- // UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
- //
- // }];
- //
- // [aler addAction:library];
- // [aler addAction:camera];
- // [aler addAction:cancel];
- //
- // [self presentViewController:aler animated:YES completion:nil];
- [self showCamera];
- }
- #pragma mark - Image Picker Delegate
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
- {
- UIImage *image = nil;
- if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
-
- image = [info objectForKey:UIImagePickerControllerEditedImage];
- [picker dismissViewControllerAnimated:YES completion:nil];
-
- } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
-
- image = [info objectForKey:UIImagePickerControllerOriginalImage];
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
- [self saveImage:image];
- [self receiveImage:image];
-
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
-
- [picker dismissViewControllerAnimated:YES completion:nil];
-
-
- }
- #pragma mark - TextField Delegate
- - (void)textFieldDidEndEditing:(UITextField *)textField {
- self.barcode = textField.text;
- }
- @end
|