ImageUploadViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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_alert:@"Please set a photo first." title:@"Upload" controller:self];
  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. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload Image" controller:self] ;
  199. }
  200. return;
  201. }
  202. #endif
  203. #ifdef OFFLINE_MODE
  204. if(appDelegate.offline_mode)
  205. {
  206. NSData *data = [OLDataProvider offline_saveBusinesscard:UIImageJPEGRepresentation(self.imgview.image,1.0f)];;
  207. // 再将NSData转为字符串
  208. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  209. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  210. DebugLog(@"data string: %@",jsonStr);
  211. NSError *error=nil;
  212. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
  213. if([[json valueForKey:@"result"] intValue]==2)
  214. {
  215. NSString* img_url_down = json[@"img_url_aname"];
  216. NSString* img_url_up = json[@"img_url"];
  217. if(self.returnValue)
  218. self.returnValue(img_url_down,img_url_up,self.imgview.image);
  219. [self.navigationController popViewControllerAnimated:false];
  220. }
  221. else
  222. {
  223. [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload Image" controller:self] ;
  224. }
  225. }
  226. else
  227. #endif
  228. {
  229. // UIAlertView * waitalert = [[UIAlertView alloc] initWithTitle:@"wait" message:@"uploading" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
  230. // [waitalert show];
  231. #ifdef GOOGLE_ANALYTICS
  232. NSString *const ScreenCodeUploadImage = @"Upload Image Screen";
  233. [ERPUtils googleAnalyticsSendRequestString:nil WithScreen:ScreenCodeUploadImage Action:nil Extra:nil];
  234. #endif
  235. NSData *imageData = UIImagePNGRepresentation(self.imgview.image);
  236. NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
  237. if(appDelegate.user!=nil)
  238. [params setValue:appDelegate.user forKey:@"user"];
  239. if(appDelegate.password!=nil)
  240. [params setValue:appDelegate.password forKey:@"password"];
  241. // AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
  242. //
  243. // NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_IMG parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  244. // [formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test.jpg" mimeType:@"image/png"];
  245. // } error:nil];
  246. //
  247. //
  248. // // NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:WEBSERVICE_URL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  249. // // [formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test" mimeType:@"image/png"];
  250. // // } error:nil];
  251. // //
  252. // AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  253. //
  254. //
  255. //
  256. // //manager.responseSerializer.acceptableContentTypes = [self.operationManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
  257. //
  258. //
  259. //
  260. // //
  261. // NSProgress *progress = nil;
  262. //
  263. // NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  264. //
  265. // // [progress removeObserver:self forKeyPath:@"fractionCompleted"];
  266. //
  267. //
  268. //
  269. //
  270. // if (error) {
  271. //
  272. // NSString* err_msg = [error localizedDescription];
  273. // DebugLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
  274. //
  275. //
  276. // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  277. //
  278. // DebugLog(@"data string: %@",str);
  279. // [RAUtils message_alert:@"Can not connect to server please try again." title:@"Upload Image" controller:self] ;
  280. //
  281. // } else {
  282. // DebugLog(@"response ");
  283. //
  284. //
  285. //
  286. // NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
  287. //
  288. // // 再将NSData转为字符串
  289. // NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  290. //
  291. // // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  292. //
  293. // DebugLog(@"data string: %@",jsonStr);
  294. //
  295. // NSDictionary* json = responseObject;
  296. //
  297. //
  298. // if([[json valueForKey:@"result"] intValue]==2)
  299. // {
  300. // NSString* img_url_down = json[@"img_url_aname"];
  301. // NSString* img_url_up = json[@"img_url"];
  302. //
  303. // if(self.returnValue)
  304. // self.returnValue(img_url_down,img_url_up,self.imgview.image);
  305. //
  306. // [self.navigationController popViewControllerAnimated:false];
  307. // }
  308. // else
  309. // {
  310. // [RAUtils message_alert:[json valueForKey:@"err_msg"] title:@"Upload Image" controller:self] ;
  311. // }
  312. //
  313. //
  314. // }
  315. // // [waitalert dismissViewControllerAnimated:YES completion:nil];
  316. // }];
  317. //
  318. //
  319. //
  320. //
  321. // [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
  322. // //
  323. //
  324. //
  325. //
  326. //
  327. //
  328. //
  329. // [uploadTask resume];
  330. __weak typeof(self) weakSelf = self;
  331. [NetworkUtils upload:imageData FileName:@"test.jpg" Params:params ToHost:URL_UPLOAD_IMG Result:^(NSMutableDictionary *result) {
  332. if([[result valueForKey:@"result"] intValue]==2)
  333. {
  334. NSString* img_url_down = result[@"img_url_aname"];
  335. NSString* img_url_up = result[@"img_url"];
  336. if(weakSelf.returnValue)
  337. weakSelf.returnValue(img_url_down,img_url_up,self.imgview.image);
  338. [weakSelf.navigationController popViewControllerAnimated:false];
  339. }
  340. else
  341. {
  342. [RAUtils message_alert:[result valueForKey:@"err_msg"] title:@"Upload Image" controller:weakSelf] ;
  343. }
  344. } Progress:^(NSURLSessionTask *task, double progress) {
  345. dispatch_async(dispatch_get_main_queue(), ^{
  346. [weakSelf.uploadProgress setProgress:progress animated:YES];
  347. });
  348. } DecryptHandler:nil];
  349. }
  350. }
  351. #pragma mark - imagePicker delegate
  352. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  353. self.dirty = true;
  354. /*-------------------------------相机拍照--------------------------------------*/
  355. if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
  356. UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
  357. image=[RAUtils img_compress:image kbsize:500];
  358. self.imgview.image = image;
  359. [picker dismissViewControllerAnimated:YES completion:^{
  360. }];
  361. } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
  362. /**
  363. * UIImagePickerControllerEditedImage 取得的是被编辑过的图片,需要allowsEditing设置为YES
  364. * UIImagePickerControllerOriginalImage 取得的是相册中原始图片
  365. */
  366. UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
  367. image=[RAUtils img_compress:image kbsize:500];
  368. self.imgview.image = image;
  369. [picker dismissViewControllerAnimated:YES completion:nil];
  370. }
  371. }
  372. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  373. [picker dismissViewControllerAnimated:YES completion:nil];
  374. // [picker presentViewController:cameraPicker animated:YES completion:nil];
  375. DebugLog(@"%s",__FUNCTION__);
  376. }
  377. /*
  378. #pragma mark - Navigation
  379. // In a storyboard-based application, you will often want to do a little preparation before navigation
  380. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  381. // Get the new view controller using [segue destinationViewController].
  382. // Pass the selected object to the new view controller.
  383. }
  384. */
  385. @end