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 ><span >UIImagePickerController</span><span > *picker = [[</span><span >UIImagePickerController</span><span > </span><span >alloc</span><span >] </span><span >init</span><span >];</span></p><p ><span > </span><span >if</span><span > ([</span><span >UIImagePickerController</span><span > </span><span >isSourceTypeAvailable</span><span >:</span></p><p ><span > </span><span >UIImagePickerControllerSourceTypePhotolibrary</span><span >]) {</span></p><p ><span > picker.</span><span >sourceType</span><span > = </span><span >UIImagePickerControllerSourceTypePhotolibrary</span><span >;</span></p><p ><span > picker.</span><span >delegate</span><span > = </span><span >_vIEwController</span><span >;</span></p><p ><span > [picker </span><span >setAllowsEditing</span><span >:</span><span >YES</span><span >];</span></p><p ><span > </span><span >//</span><span >针对</span><span >Ipad</span><span >单独处理,否则会出错</span><span > #define isPad (UI_USER_INTERFACE_IdioM() == UIUserInterfaceIdiomPad)</span></p><p ><span > </span><span >if</span><span > (</span><span >isPad</span><span >)</span></p><p ><span > {</span></p><p ><span > </span><span >//popoverController</span><span >属性需要定义全局属性</span><span > @property(nonatomic,strong) UIPopoverController *popoverController;</span></p><p ><span > </span><span >UIPopoverController</span><span > *popover = [[</span><span >UIPopoverController</span><span > </span><span >alloc</span><span >] </span><span >initWithContentVIEwController</span><span >:picker];</span></p><p ><span > </span><span >self</span><span >.</span><span >popoverController</span><span > = popover;</span></p><p ><span > </span><span >//popoverController.delegate = self;</span></p><p ><span > </span></p><p ><span > [</span><span >self</span><span >.</span><span >popoverController</span><span > </span><span >presentPopoverFromrect</span><span >:</span><span >CGRectMake</span><span >(</span><span >0</span><span >,</span><span >0</span><span >,</span><span >300</span><span >,</span><span >300</span><span >) </span><span >inVIEw</span><span >:</span><span >_vIEwController</span><span >.</span><span >vIEw</span><span > </span><span >permittedArrowDirections</span><span >:</span><span >UIPopoverArrowDirectionAny</span><span > </span><span >animated</span><span >:</span><span >YES</span><span >];</span></p><p ><span > </span></p><p ><span > </span><span >//[self presentModalVIEwController:m_imagePicker animated:YES];</span></p><p ><span > [popover </span><span >release</span><span >];</span></p><p ><span > [picker </span><span >release</span><span >];</span></p><p ><span > }</span></p><p ><span > </span><span >else</span><span >{</span></p><p ><span > [</span><span >_vIEwController</span><span > </span><span >presentModalVIEwController</span><span >:picker </span><span >animated</span><span >:</span><span >YES</span><span >];</span></p><p ><span > }</span></p><p ><span > </span></p><p ><span > }</span><span >else</span><span > {</span></p><p ><span > </span><span >UIAlertVIEw</span><span > *alert = [[</span><span >UIAlertVIEw</span><span > </span><span >alloc</span><span >]</span><span >initWithTitle</span><span >:</span><span >nil</span><span > </span><span >message</span><span >:</span><span >@"Error accessing photo library!"</span></p><p ><span > </span><span >delegate</span><span >:</span><span >nil</span><span > </span><span >cancelbuttonTitle</span><span >:</span><span >@"Close"</span><span > </span><span >otherbuttonTitles</span><span >:</span><span >nil</span><span >];</span></p><p ><span > [alert </span><span >show</span><span >];</span></p><p ><span > [alert </span><span >release</span><span >]; </span></p><p ><span > }</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]; }总结
以上是内存溢出为你收集整理的cocos2d-x横版项目调用 ios UIImagePickerController全部内容,希望文章能够帮你解决cocos2d-x横版项目调用 ios UIImagePickerController所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)