ImageUploadViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. //
  2. // ImageUploadViewController.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 11/4/15.
  6. // Copyright © 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "ImageUploadViewController.h"
  9. #import "config.h"
  10. #import "RAUtils.h"
  11. #import "AppDelegate.h"
  12. #ifdef OFFLINE_MODE
  13. #import "OLDataProvider.h"
  14. #endif
  15. #ifdef GOOGLE_ANALYTICS
  16. #import "ERPUtils.h"
  17. #endif
  18. //#import "iSalesNetwork.h"
  19. #import "NetworkUtils.h"
  20. #import "OLDataProvider.h"
  21. #define TEST_URL @"http://192.168.1.3:8080/ios_server/ul.jsp"
  22. @interface ImageUploadViewController ()
  23. @end
  24. @implementation ImageUploadViewController
  25. //-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  26. //{
  27. //// UIApplication * app = [UIApplication sharedApplication];
  28. //// AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  29. //// [appDelegate didRotated];
  30. //
  31. // // [CATransaction begin];
  32. // // [CATransaction setAnimationDuration:0.5];
  33. // // [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  34. // // // [self updatePreviewLayerForOrientation:toInterfaceOrientation];
  35. // // _previewLayer.position = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
  36. // //
  37. // // // self.backgroundView.highLightRect = CGRectInset(self.focusZone.frame,6,6);
  38. // // _previewLayer.bounds = rect;
  39. // //
  40. // // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  41. // // switch (orientation) {
  42. // //
  43. // //
  44. // //
  45. // // case UIInterfaceOrientationPortrait:
  46. // // // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(0)];
  47. // //
  48. // // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortrait;
  49. // // break;
  50. // // case UIInterfaceOrientationPortraitUpsideDown:
  51. // // //[_previewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI)];
  52. // // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortraitUpsideDown;
  53. // // break;
  54. // // case UIInterfaceOrientationLandscapeLeft:
  55. // // // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI/2)];
  56. // // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeLeft;
  57. // // break;
  58. // // case UIInterfaceOrientationLandscapeRight:
  59. // // // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(-M_PI/2)];
  60. // // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeRight;
  61. // // break;
  62. // // default:
  63. // // break;
  64. // // }
  65. // // [CATransaction commit];
  66. //
  67. // // 2018.12.28 iPad Pro 11-inch位置不正确
  68. //// NSTimeInterval animationDuration = 0.30f;
  69. //// [UIView beginAnimations:nil context:nil];
  70. //// [UIView setAnimationDuration:animationDuration];
  71. ////
  72. //// UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  73. //// if (UIInterfaceOrientationIsLandscape(orientation))
  74. //// {
  75. ////
  76. ////
  77. //// self.buttonAlbum.frame = CGRectMake(942,120,48,48);
  78. //// self.buttonCamera.frame = CGRectMake(942,200,48,48);
  79. //// self.buttonUpload.frame = CGRectMake(942,363,48,48);
  80. //// // @property (strong, nonatomic) IBOutlet UIButton *buttonAlbum;
  81. //// // @property (strong, nonatomic) IBOutlet UIButton *buttonCamera;
  82. //// // @property (strong, nonatomic) IBOutlet UIButton *buttonUpload;
  83. //// }
  84. //// else
  85. //// {
  86. //// self.buttonAlbum.frame = CGRectMake(82,918,48,48);
  87. //// self.buttonCamera.frame = CGRectMake(173,918,48,48);
  88. //// self.buttonUpload.frame = CGRectMake(635,918,48,48);
  89. ////
  90. //// }
  91. ////
  92. //// [UIView commitAnimations];
  93. //
  94. //
  95. //
  96. //
  97. //
  98. //
  99. //}
  100. - (void)viewDidLoad {
  101. [super viewDidLoad];
  102. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  103. style:UIBarButtonItemStylePlain
  104. target:self
  105. action:@selector( onBackClick:)];
  106. self.navigationItem.leftBarButtonItem = closeButton;
  107. self.imgview.image = self.img;
  108. // UIApplication * app = [UIApplication sharedApplication];
  109. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  110. // [appDelegate didRotated];
  111. // 2018.12.28 iPad Pro 11-inch位置不正确
  112. // NSTimeInterval animationDuration = 0.30f;
  113. // [UIView beginAnimations:nil context:nil];
  114. // [UIView setAnimationDuration:animationDuration];
  115. //
  116. // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  117. // if (UIInterfaceOrientationIsLandscape(orientation))
  118. // {
  119. //
  120. //
  121. // self.buttonAlbum.frame = CGRectMake(942,120,48,48);
  122. // self.buttonCamera.frame = CGRectMake(942,200,48,48);
  123. // self.buttonUpload.frame = CGRectMake(942,363,48,48);
  124. // // @property (strong, nonatomic) IBOutlet UIButton *buttonAlbum;
  125. // // @property (strong, nonatomic) IBOutlet UIButton *buttonCamera;
  126. // // @property (strong, nonatomic) IBOutlet UIButton *buttonUpload;
  127. // }
  128. // else
  129. // {
  130. // self.buttonAlbum.frame = CGRectMake(82,918,48,48);
  131. // self.buttonCamera.frame = CGRectMake(173,918,48,48);
  132. // self.buttonUpload.frame = CGRectMake(635,918,48,48);
  133. //
  134. // }
  135. //
  136. // [UIView commitAnimations];
  137. // Do any additional setup after loading the view.
  138. }
  139. - (void)onBackClick:(UIButton *)sender {
  140. [self.navigationController popViewControllerAnimated:FALSE];
  141. }
  142. - (void)didReceiveMemoryWarning {
  143. [super didReceiveMemoryWarning];
  144. // Dispose of any resources that can be recreated.
  145. }
  146. - (IBAction)onClickAlbum:(id)sender {
  147. NSUInteger sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
  148. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  149. imagePickerController.delegate = self;
  150. imagePickerController.allowsEditing = YES;
  151. imagePickerController.sourceType = sourceType;
  152. [self presentViewController:imagePickerController animated:YES completion:^{}];
  153. }
  154. - (IBAction)onClickCamera:(id)sender {
  155. if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  156. {
  157. return;
  158. }
  159. NSUInteger sourceType=UIImagePickerControllerSourceTypeCamera;
  160. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  161. imagePickerController.delegate = self;
  162. imagePickerController.allowsEditing = YES;
  163. imagePickerController.sourceType = sourceType;
  164. [self presentViewController:imagePickerController animated:YES completion:^{}];
  165. }
  166. - (IBAction)onClickUpload:(id)sender {
  167. if(self.dirty==false)
  168. {
  169. [RAUtils message_box:@"Upload" message:@"Please set a photo first." completion:nil];
  170. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Upload", nil) message:NSLocalizedString(@"Please set a photo first.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Ok", nil) otherButtonTitles:nil, nil];
  171. //
  172. // // alert.
  173. // [alert show];
  174. return;
  175. }
  176. self.uploadProgress.progress = 0;
  177. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  178. #ifdef SCANNER_ORDER
  179. // if(RASingleton.sharedInstance.enable_OfflineOrder)
  180. {
  181. NSData *data = [OLDataProvider offline_saveBusinesscard:UIImageJPEGRepresentation(self.imgview.image,1.0f)];;
  182. // 再将NSData转为字符串
  183. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  184. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  185. DebugLog(@"data string: %@",jsonStr);
  186. NSError *error=nil;
  187. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
  188. if([[json valueForKey:@"result"] intValue]==2)
  189. {
  190. NSString* img_url_down = json[@"img_url_aname"];
  191. NSString* img_url_up = json[@"img_url"];
  192. if(self.returnValue)
  193. self.returnValue(img_url_down,img_url_up,self.imgview.image);
  194. [self.navigationController popViewControllerAnimated:false];
  195. }
  196. else
  197. {
  198. ;
  199. [RAUtils message_box:@"Upload Image" message:[json valueForKey:@"err_msg"] completion:nil];
  200. }
  201. return;
  202. }
  203. #endif
  204. #ifdef OFFLINE_MODE
  205. if(appDelegate.offline_mode)
  206. {
  207. NSData *data = [OLDataProvider offline_saveBusinesscard:UIImageJPEGRepresentation(self.imgview.image,1.0f)];;
  208. // 再将NSData转为字符串
  209. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  210. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  211. DebugLog(@"data string: %@",jsonStr);
  212. NSError *error=nil;
  213. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
  214. if([[json valueForKey:@"result"] intValue]==2)
  215. {
  216. NSString* img_url_down = json[@"img_url_aname"];
  217. NSString* img_url_up = json[@"img_url"];
  218. if(self.returnValue)
  219. self.returnValue(img_url_down,img_url_up,self.imgview.image);
  220. [self.navigationController popViewControllerAnimated:false];
  221. }
  222. else
  223. {
  224. ;
  225. [RAUtils message_box:@"Upload Image" message:[json valueForKey:@"err_msg"] completion:nil];
  226. }
  227. }
  228. else
  229. #endif
  230. {
  231. // UIAlertView * waitalert = [[UIAlertView alloc] initWithTitle:@"wait" message:@"uploading" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
  232. // [waitalert show];
  233. #ifdef GOOGLE_ANALYTICS
  234. NSString *const ScreenCodeUploadImage = @"Upload Image Screen";
  235. [ERPUtils googleAnalyticsSendRequestString:nil WithScreen:ScreenCodeUploadImage Action:nil Extra:nil];
  236. #endif
  237. NSData *imageData = UIImagePNGRepresentation(self.imgview.image);
  238. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  239. if(appDelegate.user!=nil)
  240. [params setValue:appDelegate.user forKey:@"user"];
  241. if(appDelegate.password!=nil)
  242. [params setValue:appDelegate.password forKey:@"password"];
  243. __weak typeof(self) weakSelf = self;
  244. [NetworkUtils upload:imageData FileName:@"test.jpg" Params:params ToHost:URL_UPLOAD_IMG Result:^(NSMutableDictionary *result) {
  245. if([[result valueForKey:@"result"] intValue]==2)
  246. {
  247. NSString* img_url_down = result[@"img_url_aname"];
  248. NSString* img_url_up = result[@"img_url"];
  249. if(weakSelf.returnValue)
  250. weakSelf.returnValue(img_url_down,img_url_up,self.imgview.image);
  251. [weakSelf.navigationController popViewControllerAnimated:false];
  252. }
  253. else
  254. {
  255. [RAUtils message_box:@"Upload Image" message:[result valueForKey:@"err_msg"] completion:nil];
  256. }
  257. } Progress:^(NSURLSessionTask *task, double progress) {
  258. dispatch_async(dispatch_get_main_queue(), ^{
  259. [weakSelf.uploadProgress setProgress:progress animated:YES];
  260. });
  261. } DecryptHandler:nil];
  262. }
  263. }
  264. #pragma mark - imagePicker delegate
  265. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  266. self.dirty = true;
  267. /*-------------------------------相机拍照--------------------------------------*/
  268. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  269. UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
  270. image=[RAUtils img_compress:image kbsize:500];
  271. self.imgview.image = image;
  272. [picker dismissViewControllerAnimated:YES completion:^{
  273. }];
  274. } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
  275. /**
  276. * UIImagePickerControllerEditedImage 取得的是被编辑过的图片,需要allowsEditing设置为YES
  277. * UIImagePickerControllerOriginalImage 取得的是相册中原始图片
  278. */
  279. UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
  280. image=[RAUtils img_compress:image kbsize:500];
  281. self.imgview.image = image;
  282. [picker dismissViewControllerAnimated:YES completion:nil];
  283. }
  284. }
  285. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  286. [picker dismissViewControllerAnimated:YES completion:nil];
  287. // [picker presentViewController:cameraPicker animated:YES completion:nil];
  288. DebugLog(@"%s",__FUNCTION__);
  289. }
  290. /*
  291. #pragma mark - Navigation
  292. // In a storyboard-based application, you will often want to do a little preparation before navigation
  293. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  294. // Get the new view controller using [segue destinationViewController].
  295. // Pass the selected object to the new view controller.
  296. }
  297. */
  298. @end