| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329 |
- //
- // ImageUploadViewController.m
- // RedAnt ERP Mobile
- //
- // Created by Ray on 11/4/15.
- // Copyright © 2015 United Software Applications, Inc. All rights reserved.
- //
- #import "ImageUploadViewController.h"
- #import "UIProgressView+AFNetworking.h"
- #import "AFHTTPSessionManager.h"
- #import "config.h"
- #import "RAUtils.h"
- #import "AppDelegate.h"
- //#import "UIAlertView+AFNetworking.h"
- #define TEST_URL @"http://192.168.1.3:8080/ios_server/ul.jsp"
- @interface ImageUploadViewController ()
- @end
- @implementation ImageUploadViewController
- -(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
- {
- UIApplication * app = [UIApplication sharedApplication];
- AppDelegate *appDelegate = (AppDelegate *)[app delegate];
- [appDelegate didRotated];
-
- // [CATransaction begin];
- // [CATransaction setAnimationDuration:0.5];
- // [CATransaction setAnimationTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
- // // [self updatePreviewLayerForOrientation:toInterfaceOrientation];
- // _previewLayer.position = CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
- //
- // // self.backgroundView.highLightRect = CGRectInset(self.focusZone.frame,6,6);
- // _previewLayer.bounds = rect;
- //
- // UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
- // switch (orientation) {
- //
- //
- //
- // case UIInterfaceOrientationPortrait:
- // // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(0)];
- //
- // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortrait;
- // break;
- // case UIInterfaceOrientationPortraitUpsideDown:
- // //[_previewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI)];
- // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationPortraitUpsideDown;
- // break;
- // case UIInterfaceOrientationLandscapeLeft:
- // // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(M_PI/2)];
- // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeLeft;
- // break;
- // case UIInterfaceOrientationLandscapeRight:
- // // [_previewLayer setAffineTransform:CGAffineTransformMakeRotation(-M_PI/2)];
- // _previewLayer.connection.videoOrientation=AVCaptureVideoOrientationLandscapeRight;
- // break;
- // default:
- // break;
- // }
- // [CATransaction commit];
-
-
- NSTimeInterval animationDuration = 0.30f;
- [UIView beginAnimations:nil context:nil];
- [UIView setAnimationDuration:animationDuration];
- UIInterfaceOrientation orientation = [[UIApplication sharedApplication]statusBarOrientation];
- if (UIInterfaceOrientationIsLandscape(orientation))
- {
-
-
- self.buttonAlbum.frame = CGRectMake(942,120,48,48);
- self.buttonCamera.frame = CGRectMake(942,200,48,48);
- self.buttonUpload.frame = CGRectMake(942,363,48,48);
- // @property (strong, nonatomic) IBOutlet UIButton *buttonAlbum;
- // @property (strong, nonatomic) IBOutlet UIButton *buttonCamera;
- // @property (strong, nonatomic) IBOutlet UIButton *buttonUpload;
- }
- else
- {
- self.buttonAlbum.frame = CGRectMake(82,918,48,48);
- self.buttonCamera.frame = CGRectMake(173,918,48,48);
- self.buttonUpload.frame = CGRectMake(635,918,48,48);
-
- }
-
- [UIView commitAnimations];
-
-
-
-
-
-
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
-
-
- UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
- style:UIBarButtonItemStylePlain
- target:self
- action:@selector( onBackClick:)];
-
-
- self.navigationItem.leftBarButtonItem = closeButton;
-
- self.imgview.image = self.img;
- // Do any additional setup after loading the view.
- }
- - (void)onBackClick:(UIButton *)sender {
-
-
- [self.navigationController popViewControllerAnimated:FALSE];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)onClickAlbum:(id)sender {
-
- NSUInteger sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
- UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
-
- imagePickerController.delegate = self;
-
- imagePickerController.allowsEditing = YES;
-
- imagePickerController.sourceType = sourceType;
-
- [self presentViewController:imagePickerController animated:YES completion:^{}];
- }
- - (IBAction)onClickCamera:(id)sender {
-
- if(![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
- {
- return;
- }
-
- NSUInteger sourceType=UIImagePickerControllerSourceTypeCamera;
- UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
-
- imagePickerController.delegate = self;
-
- imagePickerController.allowsEditing = YES;
-
- imagePickerController.sourceType = sourceType;
-
- [self presentViewController:imagePickerController animated:YES completion:^{}];
-
- }
- - (IBAction)onClickUpload:(id)sender {
-
-
-
-
- if(self.dirty==false)
- {
-
- 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];
-
- // alert.
- [alert show];
- return;
-
- }
- self.uploadProgress.progress = 0;
-
-
- // UIAlertView * waitalert = [[UIAlertView alloc] initWithTitle:@"wait" message:@"uploading" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
- // [waitalert show];
-
- NSData *imageData = UIImagePNGRepresentation(self.imgview.image);
-
- AFHTTPRequestSerializer* serializer=[AFHTTPRequestSerializer serializer] ;
-
-
-
- NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
- AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- if(appDelegate.user!=nil)
- [params setValue:appDelegate.user forKey:@"user"];
- // if(appDelegate.contact_id!=nil)
- // [params setValue:appDelegate.contact_id forKey:@"contactId"];
- if(appDelegate.password!=nil)
- [params setValue:appDelegate.password forKey:@"password"];
-
- NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:URL_UPLOAD_IMG parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
- [formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test.jpg" mimeType:@"image/png"];
- } error:nil];
-
-
- // NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:WEBSERVICE_URL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
- // [formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test" mimeType:@"image/png"];
- // } error:nil];
- //
- AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
-
-
-
- //manager.responseSerializer.acceptableContentTypes = [self.operationManager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
-
-
-
- //
- NSProgress *progress = nil;
-
- NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
-
- // [progress removeObserver:self forKeyPath:@"fractionCompleted"];
-
-
-
-
- if (error) {
-
- NSString* err_msg = [error localizedDescription];
- NSLog(@"%@",err_msg);// [self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
-
-
- NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
-
- NSLog(@"data string: %@",str);
-
- } else {
- NSLog(@"response ");
-
-
-
- NSData *data = [NSJSONSerialization dataWithJSONObject:(NSDictionary*)responseObject options:kNilOptions error:nil];
-
- // 再将NSData转为字符串
- NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
-
- // NSMutableString *str = [[NSMutableString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
-
- NSLog(@"data string: %@",jsonStr);
-
- NSDictionary* json = responseObject;
-
- NSString* img_url_down = json[@"img_url_aname"];
- NSString* img_url_up = json[@"img_url"];
-
- if(self.returnValue)
- self.returnValue(img_url_down,img_url_up,self.imgview.image);
-
- [self.navigationController popViewControllerAnimated:false];
-
- }
- // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
- }];
-
-
-
- [self.uploadProgress setProgressWithUploadProgressOfTask:uploadTask animated:true];
- //
-
-
-
-
-
-
- [uploadTask resume];
-
- }
- #pragma mark - imagePicker delegate
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
- self.dirty = true;
- /*-------------------------------相机拍照--------------------------------------*/
- if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
-
- UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
-
- image=[RAUtils img_compress:image kbsize:500];
- self.imgview.image = image;
- [picker dismissViewControllerAnimated:YES completion:^{
- }];
-
- } else if (picker .sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
-
- /**
- * UIImagePickerControllerEditedImage 取得的是被编辑过的图片,需要allowsEditing设置为YES
- * UIImagePickerControllerOriginalImage 取得的是相册中原始图片
- */
- UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage];
-
- image=[RAUtils img_compress:image kbsize:500];
- self.imgview.image = image;
- [picker dismissViewControllerAnimated:YES completion:nil];
- }
-
-
- }
- - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
-
- [picker dismissViewControllerAnimated:YES completion:nil];
- // [picker presentViewController:cameraPicker animated:YES completion:nil];
- NSLog(@"%s",__FUNCTION__);
- }
- /*
- #pragma mark - Navigation
- // In a storyboard-based application, you will often want to do a little preparation before navigation
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
- // Get the new view controller using [segue destinationViewController].
- // Pass the selected object to the new view controller.
- }
- */
- @end
|