cocos2d-x的一个横版项目需呀用到UIImagePickerController
ios直接报错,原因是不支持横版
最初自己写了ImagePickerViewController专门的ViewController来控制这块,通过addSubview竖屏没问题,但是在横屏有很多的问题
通过paush或者presentModalViewController切换之后,取得图片之后返回游戏,直接报错OpenGL错误
强制限制ImagePickerViewController的横竖屏都没有有效解决该问题,后来直接在RootViewController添加UIImagePickerController就好了
解决办法
1、Targets > General > Deployment Info
Device Orientation 选中 Portrait 与 Landscape Left Landscape Right
2、代码RootViewController.h
#import <UIKit/UIKit.h> @interface RootViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate> { } - (BOOL) prefeRSStatusBarHidden; @end
代码RootViewController.mm
#import "RootViewController.h" #import "cocos2d.h" #import "platform/ios/CCEAGLView-ios.h" #include "SimpleAudioEngine.h" using namespace CocosDenshion; @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 { } */ /* // Implement viewDidLoad to do additional setup after loading the view,typically from a nib. - (void)viewDidLoad { [super viewDidLoad]; } */ // Override to allow orientations other than the default portrait orientation. // This method is deprecated on ios6 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return UIInterfaceOrientationIsLandscape( interfaceOrientation ); } // For ios6,use supportedInterfaceOrientations & shouldAutorotate instead //- (NSUInteger) supportedInterfaceOrientations{ //#ifdef __IPHONE_6_0 // return UIInterfaceOrientationMaskAllButUpsideDown; //#endif //} - (NSUInteger) supportedInterfaceOrientations { //Because your app is only landscape,your view controller for the view in your // popover needs to support only landscape return UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight; } - (BOOL) shouldAutorotate { return YES; } //- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation //{ // return UIInterfaceOrientationPortrait; //} - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; auto glview = cocos2d::Director::getInstance()->getOpenGLView(); if (glview) { CCEAGLView *eaglview = (CCEAGLView*) glview->getEAGLView(); if (eaglview) { CGSize s = CGSizeMake([eaglview getWidth],[eaglview getHeight]); cocos2d::Application::getInstance()->applicationScreenSizeChanged((int) s.width,(int) s.height); } } } //fix not hide status on ios7 - (BOOL)prefeRSStatusBarHidden { 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. } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (void)dealloc { [super dealloc]; } #pragma 拍照选择照片协议方法 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { [UIApplication sharedApplication].statusBarHidden = NO; NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; if ([mediaType isEqualToString:@"public.image"]){ //先把图片转成NSData // UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; UIImage* imageSrc = [info objectForKey:@"UIImagePickerControllerEditedImage"]; float scale = 140.0f / imageSrc.size.width; UIImage* image = [self scaleImage:imageSrc toScale:scale]; NSData *data; if (UIImagePNGRepresentation(image) == nil) { data = UIImageJPEGRepresentation(image,1.0); } else { data = UIImagePNGRepresentation(image); } //图片保存的路径 //这里将图片放在沙盒的documents文件夹中 NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; //文件管理器 NSFileManager *fileManager = [NSFileManager defaultManager]; //生成唯一字符串 NSString* uuid = [[NSUUID UUID] UUIDString]; //文件名 NSString* fileName = [NSString stringWithFormat:@"/%@.png",uuid]; //把刚刚图片转换的data对象拷贝至沙盒中 并保存为XXXXXXXX-XXXX-XXXX....XXXX.png [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil]; [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:fileName] contents:data attributes:nil]; //得到选择后沙盒中图片的完整路径 NSString* filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,fileName]; //关闭相册界面 [picker dismissModalViewControllerAnimated:YES]; std::string strFilePath = [filePath UTF8String]; cocos2d::Director::getInstance()->getEventDispatcher()->dispatchCustomEvent("ImagePickerEvent",&strFilePath); } } #pragma mark- 缩放图片 -(UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize { UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize)); [image drawInRect:CGRectMake(0,image.size.width * scaleSize,image.size.height *scaleSize)]; UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return scaledImage; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; cocos2d::Director::getInstance()->pause(); cocos2d::Director::getInstance()->stopAnimation(); SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; cocos2d::Director::getInstance()->resume(); cocos2d::Director::getInstance()->startAnimation(); SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); } @end
3、AppController.mm
-(void)openPhoto { // ImagePickerViewController* imagePickerViewController = [[ImagePickerViewController alloc] initWithNibName:nil bundle:nil]; // // [_viewController.view addSubview:imagePickerViewController.view]; // // [imagePickerViewController localPhoto]; <p class="p1"><span class="s1">UIImagePickerController</span><span class="s2"> *picker = [[</span><span class="s1">UIImagePickerController</span><span class="s2"> </span><span class="s1">alloc</span><span class="s2">] </span><span class="s1">init</span><span class="s2">];</span></p><p class="p1"><span class="s2"> </span><span class="s3">if</span><span class="s2"> ([</span><span class="s1">UIImagePickerController</span><span class="s2"> </span><span class="s1">isSourceTypeAvailable</span><span class="s2">:</span></p><p class="p1"><span class="s2"> </span><span class="s1">UIImagePickerControllerSourceTypePhotoLibrary</span><span class="s2">]) {</span></p><p class="p1"><span class="s2"> picker.</span><span class="s1">sourceType</span><span class="s2"> = </span><span class="s1">UIImagePickerControllerSourceTypePhotoLibrary</span><span class="s2">;</span></p><p class="p2"><span class="s1"> picker.</span><span class="s4">delegate</span><span class="s1"> = </span><span class="s5">_viewController</span><span class="s1">;</span></p><p class="p2"><span class="s1"> [picker </span><span class="s4">setAllowsEditing</span><span class="s1">:</span><span class="s3">YES</span><span class="s1">];</span></p><p class="p3"><span class="s2"> </span><span class="s1">//</span><span class="s6">针对</span><span class="s1">Ipad</span><span class="s6">单独处理,否则会出错</span><span class="s1"> #define isPad (UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPad)</span></p><p class="p2"><span class="s1"> </span><span class="s3">if</span><span class="s1"> (</span><span class="s7">isPad</span><span class="s1">)</span></p><p class="p2"><span class="s1"> {</span></p><p class="p3"><span class="s2"> </span><span class="s1">//popoverController</span><span class="s6">属性需要定义全局属性</span><span class="s1"> @property(nonatomic,strong) UIPopoverController *popoverController;</span></p><p class="p1"><span class="s2"> </span><span class="s1">UIPopoverController</span><span class="s2"> *popover = [[</span><span class="s1">UIPopoverController</span><span class="s2"> </span><span class="s1">alloc</span><span class="s2">] </span><span class="s1">initWithContentViewController</span><span class="s2">:picker];</span></p><p class="p2"><span class="s1"> </span><span class="s3">self</span><span class="s1">.</span><span class="s5">popoverController</span><span class="s1"> = popover;</span></p><p class="p3"><span class="s2"> </span><span class="s1">//popoverController.delegate = self;</span></p><p class="p4"><span class="s1"> </span></p><p class="p1"><span class="s2"> [</span><span class="s3">self</span><span class="s2">.</span><span class="s5">popoverController</span><span class="s2"> </span><span class="s1">presentPopoverFromRect</span><span class="s2">:</span><span class="s1">CGRectMake</span><span class="s2">(</span><span class="s8">0</span><span class="s2">,</span><span class="s8">0</span><span class="s2">,</span><span class="s8">300</span><span class="s2">,</span><span class="s8">300</span><span class="s2">) </span><span class="s1">inView</span><span class="s2">:</span><span class="s5">_viewController</span><span class="s2">.</span><span class="s1">view</span><span class="s2"> </span><span class="s1">permittedArrowDirections</span><span class="s2">:</span><span class="s1">UIPopoverArrowDirectionAny</span><span class="s2"> </span><span class="s1">animated</span><span class="s2">:</span><span class="s3">YES</span><span class="s2">];</span></p><p class="p4"><span class="s1"> </span></p><p class="p3"><span class="s2"> </span><span class="s1">//[self presentModalViewController:m_imagePicker animated:YES];</span></p><p class="p2"><span class="s1"> [popover </span><span class="s4">release</span><span class="s1">];</span></p><p class="p2"><span class="s1"> [picker </span><span class="s4">release</span><span class="s1">];</span></p><p class="p2"><span class="s1"> }</span></p><p class="p2"><span class="s1"> </span><span class="s3">else</span><span class="s1">{</span></p><p class="p1"><span class="s2"> [</span><span class="s5">_viewController</span><span class="s2"> </span><span class="s1">presentModalViewController</span><span class="s2">:picker </span><span class="s1">animated</span><span class="s2">:</span><span class="s3">YES</span><span class="s2">];</span></p><p class="p2"><span class="s1"> }</span></p><p class="p4"><span class="s1"> </span></p><p class="p2"><span class="s1"> }</span><span class="s3">else</span><span class="s1"> {</span></p><p class="p1"><span class="s2"> </span><span class="s1">UIAlertView</span><span class="s2"> *alert = [[</span><span class="s1">UIAlertView</span><span class="s2"> </span><span class="s1">alloc</span><span class="s2">]</span><span class="s1">initWithTitle</span><span class="s2">:</span><span class="s3">nil</span><span class="s2"> </span><span class="s1">message</span><span class="s2">:</span><span class="s9">@"Error accessing photo library!"</span></p><p class="p2"><span class="s1"> </span><span class="s4">delegate</span><span class="s1">:</span><span class="s3">nil</span><span class="s1"> </span><span class="s4">cancelButtonTitle</span><span class="s1">:</span><span class="s9">@"Close"</span><span class="s1"> </span><span class="s4">otherButtonTitles</span><span class="s1">:</span><span class="s3">nil</span><span class="s1">];</span></p><p class="p2"><span class="s1"> [alert </span><span class="s4">show</span><span class="s1">];</span></p><p class="p2"><span class="s1"> [alert </span><span class="s4">release</span><span class="s1">]; </span></p><p class="p2"><span class="s1"> }</span></p> } -(void)openCamera { // ImagePickerViewController* imagePickerViewController = [[ImagePickerViewController alloc] initWithNibName:nil bundle:nil]; // // [_viewController.view addSubview:imagePickerViewController.view]; // // [imagePickerViewController takePhoto]; UIImagePickerController * picker = [[UIImagePickerController alloc] init]; picker.delegate = _viewController; picker.allowsEditing = YES; if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { picker.sourceType = UIImagePickerControllerSourceTypeCamera; }else{ NSLog(@"模拟器无法打开相机"); } [_viewController presentModalViewController:picker animated:YES]; }原文链接:https://www.f2er.com/cocos2dx/341248.html