/**************************************************************************** Copyright (c) 2013 cocos2d-x.org Copyright (c) 2013-2016 Chukong Technologies Inc. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. http://www.cocos2d-x.org Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ****************************************************************************/ #import "RootViewController.h" #import "cocos2d.h" #include "platform/CCApplication.h" #include "platform/ios/CCEAGLView-ios.h" #import "AFHTTPRequestOperationManager.h" #include "cocos/scripting/js-bindings/jswrapper/SeApi.h" @implementation RootViewController /* // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { // Custom initialization } return self; } */ // Implement loadView to create a view hierarchy programmatically, without using a nib. - (void)loadView { // Set EAGLView as view of RootViewController self.view = (__bridge CCEAGLView *)cocos2d::Application::getInstance()->getView(); } // Implement viewDidLoad to do additional setup after loading the view, typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; RCTself= self; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; } - (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; } +(void)pickImg:(NSString *)pickImg description:(NSString *)description { // HVWOpenAlbumTool *tool = [[HVWOpenAlbumTool alloc] init]; // [tool openAlbumWithVC:self completion:^(UIImage *image) { // // }]; NSData *resData = [[NSData alloc] initWithData:[pickImg dataUsingEncoding:NSUTF8StringEncoding]]; NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil]; NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; [user setObject:jsonDic forKey:@"jsonDic"]; UIImagePickerController *picker = [[UIImagePickerController alloc] init]; picker.delegate = RCTself; // picker.view.userInteractionEnabled = YES; picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; // UIViewController *rootCtrl = [UIApplication sharedApplication].keyWindow.rootViewController; picker.allowsEditing = YES; // [[UIApplication sharedApplication].keyWindow addSubview:picker.view]; // [UIApplication sharedApplication].keyWindow.rootViewController=picker; [[self alloc] aaa:picker]; // [picker release]; NSLog(@"-(void)localPhoto();"); } //获取当前屏幕显示的viewcontroller - (UIViewController *)getCurrentVC { UIViewController *result = nil; UIWindow * window = [[UIApplication sharedApplication] keyWindow]; if (window.windowLevel != UIWindowLevelNormal) { NSArray *windows = [[UIApplication sharedApplication] windows]; for(UIWindow * tmpWin in windows) { if (tmpWin.windowLevel == UIWindowLevelNormal) { window = tmpWin; break; } } } UIView *frontView = [[window subviews] objectAtIndex:0]; id nextResponder = [frontView nextResponder]; if ([nextResponder isKindOfClass:[UIViewController class]]) result = nextResponder; else result = window.rootViewController; return result; } - (void)aaa:(UIImagePickerController*)picker{ picker.delegate = self; [[self getCurrentVC] presentViewController:picker animated:YES completion:^{ }]; // [self presentViewController:picker animated:YES completion:^(void){ // NSLog(@"Imageviewcontroller is presented"); // // }]; } - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ NSString *type = [info objectForKey:UIImagePickerControllerMediaType]; // [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRotate" object:@"0"];// 横竖屏切换通 //当选择的类型是图片 NSURL *url111 = [info objectForKey:UIImagePickerControllerReferenceURL]; NSString *str = url111.absoluteString; NSLog(@"%@", str); std::string strRet1 = [str UTF8String]; std::string strRet = "OnPhotoPickEnd"; NSLog(@"Q::%s",strRet1.c_str()); std::string jsCallStr = cocos2d::StringUtils::format("CallUploadSuccess(\"%s\");", strRet.c_str()); NSLog(@"~~~~%s",jsCallStr.c_str()); se::Value *ret = new se::Value(); se::ScriptEngine::getInstance()->evalString(jsCallStr.c_str() , -1 , ret); if ([type isEqualToString:@"public.image"]) { //先把图片转成NSData // UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; NSData *data; if (UIImagePNGRepresentation(image) == nil) { data = UIImageJPEGRepresentation(image, 0.00001); } else { data = UIImagePNGRepresentation(image); } //关闭相冊界面 NSMutableDictionary *productParams=[[NSMutableDictionary alloc]init]; NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; NSDictionary *jsonDic = [user objectForKey:@"jsonDic"]; NSArray *arrKeys = [jsonDic allKeys]; [productParams setValue:[jsonDic valueForKey:arrKeys[0]] forKey:arrKeys[0]]; [productParams setValue:[jsonDic valueForKey:arrKeys[1]] forKey:arrKeys[1]]; [productParams setValue:[jsonDic valueForKey:arrKeys[2]] forKey:arrKeys[2]]; NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *imageFileName = [NSString stringWithFormat:@"%@.jpg", [formatter stringFromDate:[NSDate date]]]; [productParams setValue:imageFileName forKey:@"filename"]; for (int i = 0 ; i < arrKeys.count ; i++) { NSString *key = arrKeys[i]; NSString *value = [jsonDic valueForKey:key]; NSLog(@"%@ = %@",key,value); } NSLog(@"%zd",jsonDic.count); // 2 [jsonDic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { NSLog(@"key = %@ and obj = %@", key, obj); }]; NSString *url = [jsonDic objectForKey:@"URL"]; //两种方式上传头像 /*方式一:使用NSData数据流传图片*/ AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; manager.responseSerializer = [AFHTTPResponseSerializer serializer]; manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", @"image/jpeg", nil]; [manager POST:url parameters:productParams constructingBodyWithBlock:^(id formData) { //拼接图片名称+时间确保不重复命名 NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; formatter.dateFormat = @"yyyyMMddHHmmss"; NSString *imageFileName = [NSString stringWithFormat:@"%@.jpg", [formatter stringFromDate:[NSDate date]]]; [formData appendPartWithFileData:data name:@"file" fileName:imageFileName mimeType:@"image/jpeg"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { NSString *strtmp = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]; NSString *requestTmp = [NSString stringWithString: strtmp]; NSData *resData = [[NSData alloc] initWithData:[requestTmp dataUsingEncoding:NSUTF8StringEncoding]]; NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:resData options:NSJSONReadingMutableLeaves error:nil]; //解析 // [self.navigationController popToRootViewControllerAnimated:YES]; if ([resultDic[@"code"] intValue] == 200) { // [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRotate" object:@"0"];// 横竖屏切换通知 // [picker.view removeFromSuperview]; [picker dismissViewControllerAnimated:YES completion:^{ }]; }else{ [picker dismissViewControllerAnimated:YES completion:^{ }]; } NSLog(@"+++>%@", resultDic); NSLog(@"--->%@", resultDic[@"message"]); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSData * data = error.userInfo[@"com.alamofire.serialization.response.error.data"]; NSString * str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"服务器的错误原因:%@",str); NSLog(@"--->%@", error); }]; } } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ NSLog(@"您取消了选择图片"); // [[NSNotificationCenter defaultCenter] postNotificationName:@"changeRotate" object:@"0"];// 横竖屏切换通知 // [picker.view removeFromSuperview]; [picker dismissViewControllerAnimated:YES completion:^{ }]; } // For ios6, use supportedInterfaceOrientations & shouldAutorotate instead #ifdef __IPHONE_6_0 - (NSUInteger) supportedInterfaceOrientations{ return UIInterfaceOrientationMaskAllButUpsideDown; } #endif - (BOOL) shouldAutorotate { return YES; } //fix not hide status on ios7 - (BOOL)prefersStatusBarHidden { return YES; } // Controls the application's preferred home indicator auto-hiding when this view controller is shown. - (BOOL)prefersHomeIndicatorAutoHidden { return YES; } - (void)didReceiveMemoryWarning { // Releases the view if it doesn't have a superview. [super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use. } @end