ImageUploadViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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 "UIProgressView+AFNetworking.h"
  10. //#import "AFHTTPSessionManager.h"
  11. #import "config.h"
  12. #import "RAUtils.h"
  13. #import "AppDelegate.h"
  14. #ifdef OFFLINE_MODE
  15. #import "OLDataProvider.h"
  16. #endif
  17. #ifdef GOOGLE_ANALYTICS
  18. #import "ERPUtils.h"
  19. #endif
  20. //#import "iSalesNetwork.h"
  21. #import "NetworkUtils.h"
  22. #import "OLDataProvider.h"
  23. //#import "UIAlertView+AFNetworking.h"
  24. #define TEST_URL @"http://192.168.1.3:8080/ios_server/ul.jsp"
  25. @interface ImageUploadViewController ()
  26. @end
  27. @implementation ImageUploadViewController
  28. //-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
  29. //{
  30. //// UIApplication * app = [UIApplication sharedApplication];
  31. //// AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  32. //// [appDelegate didRotated];
  33. //
  34. // // [CATransaction begin];
  35. // // [CATransaction setAnimationDuration:0.5];
  36. // // [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
  37. // // // [self updatePreviewLayerForOrientation:toInterfaceOrientation];
  38. // // _previewLayer.position = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
  39. // //
  40. // // // self.backgroundView.highLightRect = CGRectInset(self.focusZone.frame,6,6);
  41. // // _previewLayer.bounds = rect;
  42. // //
  43. // // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  44. // // switch (orientation) {
  45. // //
  46. // //
  47. // //
  48. // // case UIInterfaceOrientationPortrait:
  49. // // // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(0)];
  50. // //
  51. // // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortrait;
  52. // // break;
  53. // // case UIInterfaceOrientationPortraitUpsideDown:
  54. // // //[_previewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI)];
  55. // // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortraitUpsideDown;
  56. // // break;
  57. // // case UIInterfaceOrientationLandscapeLeft:
  58. // // // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI/2)];
  59. // // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeLeft;
  60. // // break;
  61. // // case UIInterfaceOrientationLandscapeRight:
  62. // // // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(-M_PI/2)];
  63. // // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeRight;
  64. // // break;
  65. // // default:
  66. // // break;
  67. // // }
  68. // // [CATransaction commit];
  69. //
  70. // // 2018.12.28 iPad Pro 11-inch位置不正确
  71. //// NSTimeInterval animationDuration = 0.30f;
  72. //// [UIView beginAnimations:nil context:nil];
  73. //// [UIView setAnimationDuration:animationDuration];
  74. ////
  75. //// UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  76. //// if (UIInterfaceOrientationIsLandscape(orientation))
  77. //// {
  78. ////
  79. ////
  80. //// self.buttonAlbum.frame = CGRectMake(942,120,48,48);
  81. //// self.buttonCamera.frame = CGRectMake(942,200,48,48);
  82. //// self.buttonUpload.frame = CGRectMake(942,363,48,48);
  83. //// // @property (strong, nonatomic) IBOutlet UIButton *buttonAlbum;
  84. //// // @property (strong, nonatomic) IBOutlet UIButton *buttonCamera;
  85. //// // @property (strong, nonatomic) IBOutlet UIButton *buttonUpload;
  86. //// }
  87. //// else
  88. //// {
  89. //// self.buttonAlbum.frame = CGRectMake(82,918,48,48);
  90. //// self.buttonCamera.frame = CGRectMake(173,918,48,48);
  91. //// self.buttonUpload.frame = CGRectMake(635,918,48,48);
  92. ////
  93. //// }
  94. ////
  95. //// [UIView commitAnimations];
  96. //
  97. //
  98. //
  99. //
  100. //
  101. //
  102. //}
  103. - (void)viewDidLoad {
  104. [super viewDidLoad];
  105. UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAutomatic]
  106. style:UIBarButtonItemStylePlain
  107. target:self
  108. action:@selector( onBackClick:)];
  109. self.navigationItem.leftBarButtonItem = closeButton;
  110. self.imgview.image = self.img;
  111. // UIApplication * app = [UIApplication sharedApplication];
  112. // AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  113. // [appDelegate didRotated];
  114. // 2018.12.28 iPad Pro 11-inch位置不正确
  115. // NSTimeInterval animationDuration = 0.30f;
  116. // [UIView beginAnimations:nil context:nil];
  117. // [UIView setAnimationDuration:animationDuration];
  118. //
  119. // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
  120. // if (UIInterfaceOrientationIsLandscape(orientation))
  121. // {
  122. //
  123. //
  124. // self.buttonAlbum.frame = CGRectMake(942,120,48,48);
  125. // self.buttonCamera.frame = CGRectMake(942,200,48,48);
  126. // self.buttonUpload.frame = CGRectMake(942,363,48,48);
  127. // // @property (strong, nonatomic) IBOutlet UIButton *buttonAlbum;
  128. // // @property (strong, nonatomic) IBOutlet UIButton *buttonCamera;
  129. // // @property (strong, nonatomic) IBOutlet UIButton *buttonUpload;
  130. // }
  131. // else
  132. // {
  133. // self.buttonAlbum.frame = CGRectMake(82,918,48,48);
  134. // self.buttonCamera.frame = CGRectMake(173,918,48,48);
  135. // self.buttonUpload.frame = CGRectMake(635,918,48,48);
  136. //
  137. // }
  138. //
  139. // [UIView commitAnimations];
  140. // Do any additional setup after loading the view.
  141. }
  142. - (void)onBackClick:(UIButton *)sender {
  143. [self.navigationController popViewControllerAnimated:FALSE];
  144. }
  145. - (void)didReceiveMemoryWarning {
  146. [super didReceiveMemoryWarning];
  147. // Dispose of any resources that can be recreated.
  148. }
  149. - (IBAction)onClickAlbum:(id)sender {
  150. NSUInteger sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
  151. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  152. imagePickerController.delegate = self;
  153. imagePickerController.allowsEditing = YES;
  154. imagePickerController.sourceType = sourceType;
  155. [self presentViewController:imagePickerController animated:YES completion:^{}];
  156. }
  157. - (IBAction)onClickCamera:(id)sender {
  158. if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  159. {
  160. return;
  161. }
  162. NSUInteger sourceType=UIImagePickerControllerSourceTypeCamera;
  163. UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
  164. imagePickerController.delegate = self;
  165. imagePickerController.allowsEditing = YES;
  166. imagePickerController.sourceType = sourceType;
  167. [self presentViewController:imagePickerController animated:YES completion:^{}];
  168. }
  169. - (IBAction)onClickUpload:(id)sender {
  170. if(self.dirty==false)
  171. {
  172. [RAUtils message_alert:@"Please set a photo first." title:@"Upload" controller:self];
  173. // 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];
  174. //
  175. // // alert.
  176. // [alert show];
  177. return;
  178. }
  179. self.uploadProgress.progress = 0;
  180. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  181. #ifdef SCANNER_ORDER
  182. // if(RASingleton.sharedInstance.enable_OfflineOrder)
  183. {
  184. NSData *data = [OLDataProvider offline_saveBusinesscard:UIImageJPEGRepresentation(self.imgview.image,1.0f)];;
  185. // 再将NSData转为字符串
  186. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  187. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  188. DebugLog(@"data string: %@",jsonStr);
  189. NSError *error=nil;
  190. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
  191. if([[json valueForKey:@"result"] intValue]==2)
  192. {
  193. NSString* img_url_down = json[@"img_url_aname"];
  194. NSString* img_url_up = json[@"img_url"];
  195. if(self.returnValue)
  196. self.returnValue(img_url_down,img_url_up,self.imgview.image);
  197. [self.navigationController popViewControllerAnimated:false];
  198. }
  199. else
  200. {
  201. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload Image" controller:self] ;
  202. }
  203. return;
  204. }
  205. #endif
  206. #ifdef OFFLINE_MODE
  207. if(appDelegate.offline_mode)
  208. {
  209. NSData *data = [OLDataProvider offline_saveBusinesscard:UIImageJPEGRepresentation(self.imgview.image,1.0f)];;
  210. // 再将NSData转为字符串
  211. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  212. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  213. DebugLog(@"data string: %@",jsonStr);
  214. NSError *error=nil;
  215. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
  216. if([[json valueForKey:@"result"] intValue]==2)
  217. {
  218. NSString* img_url_down = json[@"img_url_aname"];
  219. NSString* img_url_up = json[@"img_url"];
  220. if(self.returnValue)
  221. self.returnValue(img_url_down,img_url_up,self.imgview.image);
  222. [self.navigationController popViewControllerAnimated:false];
  223. }
  224. else
  225. {
  226. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload Image" controller:self] ;
  227. }
  228. }
  229. else
  230. #endif
  231. {
  232. // UIAlertView * waitalert = [[UIAlertView alloc] initWithTitle:@"wait" message:@"uploading" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
  233. // [waitalert show];
  234. #ifdef GOOGLE_ANALYTICS
  235. NSString *const ScreenCodeUploadImage = @"Upload Image Screen";
  236. [ERPUtils googleAnalyticsSendRequestString:nil WithScreen:ScreenCodeUploadImage Action:nil Extra:nil];
  237. #endif
  238. NSData *imageData = UIImagePNGRepresentation(self.imgview.image);
  239. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  240. if(appDelegate.user!=nil)
  241. [params setValue:appDelegate.user forKey:@"user"];
  242. if(appDelegate.password!=nil)
  243. [params setValue:appDelegate.password forKey:@"password"];
  244. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  245. //
  246. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_IMG parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  247. // [formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test.jpg" mimeType:@"image/png"];
  248. // } error:nil];
  249. //
  250. //
  251. // // NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:WEBSERVICE_URL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  252. // // [formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test" mimeType:@"image/png"];
  253. // // } error:nil];
  254. // //
  255. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  256. //
  257. //
  258. //
  259. // //manager.responseSerializer.acceptableContentTypes = [self.operationManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
  260. //
  261. //
  262. //
  263. // //
  264. // NSProgress *progress = nil;
  265. //
  266. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  267. //
  268. // // [progress removeObserver:self forKeyPath:@"fractionCompleted"];
  269. //
  270. //
  271. //
  272. //
  273. // if (error) {
  274. //
  275. // NSString* err_msg = [error localizedDescription];
  276. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  277. //
  278. //
  279. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  280. //
  281. // DebugLog(@"data string: %@",str);
  282. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  283. //
  284. // } else {
  285. // DebugLog(@"response ");
  286. //
  287. //
  288. //
  289. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  290. //
  291. // // 再将NSData转为字符串
  292. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  293. //
  294. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  295. //
  296. // DebugLog(@"data string: %@",jsonStr);
  297. //
  298. // NSDictionary* json = responseObject;
  299. //
  300. //
  301. // if([[json valueForKey:@"result"] intValue]==2)
  302. // {
  303. // NSString* img_url_down = json[@"img_url_aname"];
  304. // NSString* img_url_up = json[@"img_url"];
  305. //
  306. // if(self.returnValue)
  307. // self.returnValue(img_url_down,img_url_up,self.imgview.image);
  308. //
  309. // [self.navigationController popViewControllerAnimated:false];
  310. // }
  311. // else
  312. // {
  313. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload Image" controller:self] ;
  314. // }
  315. //
  316. //
  317. // }
  318. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  319. // }];
  320. //
  321. //
  322. //
  323. //
  324. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  325. // //
  326. //
  327. //
  328. //
  329. //
  330. //
  331. //
  332. // [uploadTask resume];
  333. __weak typeof(self) weakSelf = self;
  334. [NetworkUtils upload:imageData FileName:@"test.jpg" Params:params ToHost:URL_UPLOAD_IMG Result:^(NSMutableDictionary *result) {
  335. if([[result valueForKey:@"result"] intValue]==2)
  336. {
  337. NSString* img_url_down = result[@"img_url_aname"];
  338. NSString* img_url_up = result[@"img_url"];
  339. if(weakSelf.returnValue)
  340. weakSelf.returnValue(img_url_down,img_url_up,self.imgview.image);
  341. [weakSelf.navigationController popViewControllerAnimated:false];
  342. }
  343. else
  344. {
  345. [RAUtils message_alert:[result valueForKey:@"err_msg"] title:@"Upload Image" controller:weakSelf] ;
  346. }
  347. } Progress:^(NSURLSessionTask *task, double progress) {
  348. dispatch_async(dispatch_get_main_queue(), ^{
  349. [weakSelf.uploadProgress setProgress:progress animated:YES];
  350. });
  351. } DecryptHandler:nil];
  352. }
  353. }
  354. #pragma mark - imagePicker delegate
  355. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  356. self.dirty = true;
  357. /*-------------------------------相机拍照--------------------------------------*/
  358. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  359. UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
  360. image=[RAUtils img_compress:image kbsize:500];
  361. self.imgview.image = image;
  362. [picker dismissViewControllerAnimated:YES completion:^{
  363. }];
  364. } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
  365. /**
  366. * UIImagePickerControllerEditedImage 取得的是被编辑过的图片,需要allowsEditing设置为YES
  367. * UIImagePickerControllerOriginalImage 取得的是相册中原始图片
  368. */
  369. UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
  370. image=[RAUtils img_compress:image kbsize:500];
  371. self.imgview.image = image;
  372. [picker dismissViewControllerAnimated:YES completion:nil];
  373. }
  374. }
  375. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  376. [picker dismissViewControllerAnimated:YES completion:nil];
  377. // [picker presentViewController:cameraPicker animated:YES completion:nil];
  378. DebugLog(@"%s",__FUNCTION__);
  379. }
  380. /*
  381. #pragma mark - Navigation
  382. // In a storyboard-based application, you will often want to do a little preparation before navigation
  383. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  384. // Get the new view controller using [segue destinationViewController].
  385. // Pass the selected object to the new view controller.
  386. }
  387. */
  388. @end