RootViewController.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /****************************************************************************
  2. Copyright (c) 2013 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #import "RootViewController.h"
  23. #import "cocos2d.h"
  24. #include "platform/CCApplication.h"
  25. #include "platform/ios/CCEAGLView-ios.h"
  26. #import "AFHTTPRequestOperationManager.h"
  27. #include "cocos/scripting/js-bindings/jswrapper/SeApi.h"
  28. @implementation RootViewController
  29. /*
  30. // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
  31. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  32. if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
  33. // Custom initialization
  34. }
  35. return self;
  36. }
  37. */
  38. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  39. - (void)loadView {
  40. // Set EAGLView as view of RootViewController
  41. self.view = (__bridge CCEAGLView *)cocos2d::Application::getInstance()->getView();
  42. }
  43. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  44. - (void)viewDidLoad {
  45. [super viewDidLoad];
  46. RCTself= self;
  47. }
  48. - (void)viewWillAppear:(BOOL)animated {
  49. [super viewWillAppear:animated];
  50. }
  51. - (void)viewDidDisappear:(BOOL)animated {
  52. [super viewDidDisappear:animated];
  53. }
  54. +(void)pickImg:(NSString *)pickImg description:(NSString *)description {
  55. // HVWOpenAlbumTool *tool = [[HVWOpenAlbumTool alloc] init];
  56. // [tool openAlbumWithVC:self completion:^(UIImage *image) {
  57. //
  58. // }];
  59. NSData *resData = [[NSData alloc] initWithData:[pickImg dataUsingEncoding:NSUTF8StringEncoding]];
  60. NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil];
  61. NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
  62. [user setObject:jsonDic forKey:@"jsonDic"];
  63. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  64. picker.delegate = RCTself;
  65. // picker.view.userInteractionEnabled = YES;
  66. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  67. // UIViewController *rootCtrl = [UIApplication sharedApplication].keyWindow.rootViewController;
  68. picker.allowsEditing = YES;
  69. // [[UIApplication sharedApplication].keyWindow addSubview:picker.view];
  70. // [UIApplication sharedApplication].keyWindow.rootViewController=picker;
  71. [[self alloc] aaa:picker];
  72. // [picker release];
  73. NSLog(@"-(void)localPhoto();");
  74. }
  75. //获取当前屏幕显示的viewcontroller
  76. - (UIViewController *)getCurrentVC
  77. {
  78. UIViewController *result = nil;
  79. UIWindow * window = [[UIApplication sharedApplication] keyWindow];
  80. if (window.windowLevel != UIWindowLevelNormal)
  81. {
  82. NSArray *windows = [[UIApplication sharedApplication] windows];
  83. for(UIWindow * tmpWin in windows)
  84. {
  85. if (tmpWin.windowLevel == UIWindowLevelNormal)
  86. {
  87. window = tmpWin;
  88. break;
  89. }
  90. }
  91. }
  92. UIView *frontView = [[window subviews] objectAtIndex:0];
  93. id nextResponder = [frontView nextResponder];
  94. if ([nextResponder isKindOfClass:[UIViewController class]])
  95. result = nextResponder;
  96. else
  97. result = window.rootViewController;
  98. return result;
  99. }
  100. - (void)aaa:(UIImagePickerController*)picker{
  101. picker.delegate = self;
  102. [[self getCurrentVC] presentViewController:picker animated:YES completion:^{
  103. }];
  104. // [self presentViewController:picker animated:YES completion:^(void){
  105. // NSLog(@"Imageviewcontroller is presented");
  106. //
  107. // }];
  108. }
  109. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
  110. NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
  111. // [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRotate" object:@"0"];// 横竖屏切换通
  112. //当选择的类型是图片
  113. NSURL *url111 = [info objectForKey:UIImagePickerControllerReferenceURL];
  114. NSString *str = url111.absoluteString;
  115. NSLog(@"%@", str);
  116. std::string strRet1 = [str UTF8String];
  117. std::string strRet = "OnPhotoPickEnd";
  118. NSLog(@"Q::%s",strRet1.c_str());
  119. std::string jsCallStr = cocos2d::StringUtils::format("CallUploadSuccess(\"%s\");", strRet.c_str());
  120. NSLog(@"~~~~%s",jsCallStr.c_str());
  121. se::Value *ret = new se::Value();
  122. se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret);
  123. if ([type isEqualToString:@"public.image"])
  124. {
  125. //先把图片转成NSData
  126. // UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  127. UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  128. NSData *data;
  129. if (UIImagePNGRepresentation(image) == nil)
  130. {
  131. data = UIImageJPEGRepresentation(image, 0.00001);
  132. }
  133. else
  134. {
  135. data = UIImagePNGRepresentation(image);
  136. }
  137. //关闭相冊界面
  138. NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init];
  139. NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
  140. NSDictionary *jsonDic = [user objectForKey:@"jsonDic"];
  141. NSArray *arrKeys = [jsonDic allKeys];
  142. [productParams setValue:[jsonDic valueForKey:arrKeys[0]] forKey:arrKeys[0]];
  143. [productParams setValue:[jsonDic valueForKey:arrKeys[1]] forKey:arrKeys[1]];
  144. [productParams setValue:[jsonDic valueForKey:arrKeys[2]] forKey:arrKeys[2]];
  145. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  146. formatter.dateFormat = @"yyyyMMddHHmmss";
  147. NSString *imageFileName = [NSString stringWithFormat:@"%@.jpg", [formatter stringFromDate:[NSDate date]]];
  148. [productParams setValue:imageFileName forKey:@"filename"];
  149. for (int i = 0 ; i < arrKeys.count ; i++)
  150. {
  151. NSString *key = arrKeys[i];
  152. NSString *value = [jsonDic valueForKey:key];
  153. NSLog(@"%@ = %@",key,value);
  154. }
  155. NSLog(@"%zd",jsonDic.count);
  156. // 2
  157. [jsonDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
  158. NSLog(@"key = %@ and obj = %@", key, obj);
  159. }];
  160. NSString *url = [jsonDic objectForKey:@"URL"];
  161. //两种方式上传头像
  162. /*方式一:使用NSData数据流传图片*/
  163. AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
  164. manager.responseSerializer = [AFHTTPResponseSerializer serializer];
  165. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
  166. @"text/plain",
  167. @"text/javascript",
  168. @"text/json",
  169. @"text/html",
  170. @"image/jpeg", nil];
  171. [manager POST:url parameters:productParams constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
  172. //拼接图片名称+时间确保不重复命名
  173. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  174. formatter.dateFormat = @"yyyyMMddHHmmss";
  175. NSString *imageFileName = [NSString stringWithFormat:@"%@.jpg", [formatter stringFromDate:[NSDate date]]];
  176. [formData appendPartWithFileData:data name:@"file" fileName:imageFileName mimeType:@"image/jpeg"];
  177. } success:^(AFHTTPRequestOperation *operation, id responseObject) {
  178. NSString *strtmp = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
  179. NSString *requestTmp = [NSString stringWithString: strtmp];
  180. NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]];
  181. NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil]; //解析
  182. // [self.navigationController popToRootViewControllerAnimated:YES];
  183. if ([resultDic[@"code"] intValue] == 200) {
  184. // [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRotate" object:@"0"];// 横竖屏切换通知
  185. // [picker.view removeFromSuperview];
  186. [picker dismissViewControllerAnimated:YES completion:^{
  187. }];
  188. }else{
  189. [picker dismissViewControllerAnimated:YES completion:^{
  190. }];
  191. }
  192. NSLog(@"+++>%@", resultDic);
  193. NSLog(@"--->%@", resultDic[@"message"]);
  194. } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
  195. NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"];
  196. NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
  197. NSLog(@"服务器的错误原因:%@",str);
  198. NSLog(@"--->%@", error);
  199. }];
  200. }
  201. }
  202. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  203. NSLog(@"您取消了选择图片");
  204. // [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRotate" object:@"0"];// 横竖屏切换通知
  205. // [picker.view removeFromSuperview];
  206. [picker dismissViewControllerAnimated:YES completion:^{
  207. }];
  208. }
  209. // For ios6, use supportedInterfaceOrientations & shouldAutorotate instead
  210. #ifdef __IPHONE_6_0
  211. - (NSUInteger) supportedInterfaceOrientations{
  212. return UIInterfaceOrientationMaskAllButUpsideDown;
  213. }
  214. #endif
  215. - (BOOL) shouldAutorotate {
  216. return YES;
  217. }
  218. //fix not hide status on ios7
  219. - (BOOL)prefersStatusBarHidden {
  220. return YES;
  221. }
  222. // Controls the application's preferred home indicator auto-hiding when this view controller is shown.
  223. - (BOOL)prefersHomeIndicatorAutoHidden {
  224. return YES;
  225. }
  226. - (void)didReceiveMemoryWarning {
  227. // Releases the view if it doesn't have a superview.
  228. [super didReceiveMemoryWarning];
  229. // Release any cached data, images, etc that aren't in use.
  230. }
  231. @end