[英]How can i take photo in cocos2D application (iOS 6)
我知道我需要使用UIImagePickerController
從庫中拍攝照片或手動拍攝。
我在cocos2D
(iOS 6)中的應用程序,需要手動或從庫拍攝照片。
我的代碼是:
在.h文件中
@interface HelloWorldLayer : CCLayer <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIActionSheetDelegate>
在.m文件中
-(void)btnTakePhotoPressed:(UIButton *) Sender
{
UIActionSheet *actionsheet = [[UIActionSheet alloc] initWithTitle:@"Take photo" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:@"Take photo from camera", @"Select from library", nil];
[actionsheet showFromRect:Sender.frame inView:self.scrollView animated:YES];
}
#pragma Mark -
#pragma Mark - ActionSheet Delegate Methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
[[XPSModal sharedInstance] hide];
UIImagePickerController *imgPicker=[[UIImagePickerController alloc] init];
imgPicker.delegate=self;
imgPicker.wantsFullScreenLayout = YES;
imgPicker.allowsEditing=YES;
if(buttonIndex == 0)
{
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;
else
imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
else if (buttonIndex==1)
{
imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
self.popoverController = [[UIPopoverController alloc] initWithContentViewController:imgPicker];
self.popoverController.delegate = self;
[self.popoverController presentPopoverFromRect:[self.btnTakePhoto bounds] inView:[[CCDirector sharedDirector] view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *newImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
[picker dismissModalViewControllerAnimated:YES];
//[picker.view removeFromSuperview];
CCSprite *imageFromPicker = [CCSprite spriteWithCGImage:newImage.CGImage key:@"ImageFromPicker"];
[self addChild:imageFromPicker];
}
當我運行此代碼時,將顯示UIImagePickerController
,但無法選擇圖像或對圖像執行任何操作,而且我也無法刪除UIImagePickerController
。
請在此問題上幫助我,添加代碼段或有用的教程,或者給我您的黃金建議。
我猜想您的cocos2d層位於操作表的頂部。 我最近寫了一個應用程序,該應用程序具有一個UIKit層,一個Cocos2D層,然后在cocos2D層的頂部有一個UIKit層。 它花了一段時間才起作用,我為此寫了一個博客條目。 您可以在這里閱讀: http : //www.notthepainter.com/full-cocos2d-uikit-integration/
這是博客中的文字,希望對您有所幫助!
我當時正在開發基於cocos2d的敲擊游戲,但無法兩次運行我的游戲。 很明顯,我沒有正確關閉第一個游戲或沒有正確構建第二個游戲,或者沒有!
網上有很多教程,教您如何向Cocos2D應用程序添加UIKit按鈕,或如何從基於UIKit的應用程序啟動Cocos2D。 但是我需要同時做。 我想在我的游戲下放一個UIViewController,在游戲之上放一個UIKit小部件。 我花了很多時間閱讀,這就是我想出的。
首先,構建Xcode項目是一場噩夢。 我最終使用了cocos2d / box2d模板,然后撕下了不需要的文件,並將所有原始文件重新添加回去。AppDelegate.m文件看起來就像非cocos2d應用程序看起來一樣。 這與許多教程的原則背道而馳,這些教程建議您在AppDelegate中構建cocos2d環境。 我為此感到掙扎,在一個星期五的大部分時間里都沒有運氣,然后在星期一我放入了Cocos2DSingleton,它幾乎是第一次運行。
這是我的GameViewController的viewDidLoad方法:
- (void)viewDidLoad
{
[super viewDidLoad];
srandom(time(0));
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
TTCocos2DSingleton *shared = [TTCocos2DSingleton sharedCocos2D];
CCGLView *glView = [shared currentGLView];
[self.view insertSubview:glView atIndex:1];
}
這里有一些注意事項。 GameViewController具有游戲UIButton,得分UILabel和其他游戲類型的UI小部件。 這使我可以在Interface Builder中完成許多游戲控件,而不必手工布置它們。 我首先初始化隨機性,只是為隨機數生成器提供種子。 然后,由於游戲是全屏的,所以我隱藏了狀態欄。
我通過單例獲取我的cocos2d實例,獲取其glView並將其插入到索引為1的GameViewController的視圖中。這將其置於所有游戲控件的下方。 稍后,我將向您展示sharedCocos2D方法,讓我們看一下viewWillAppear。
- (void) viewWillAppear:(BOOL)animated
{
if(![[CCDirector sharedDirector] runningScene]){
CCScene *scene = [MyGameLayer scene];
myGame = [MyGameLayer node];
myGame.delegate = self;
[scene addChild: myGame];
[[CCDirector sharedDirector] runWithScene:scene];
} else {
// we have a scene already, replace the original to get a new game
[[CCDirector sharedDirector] startAnimation];
CCScene *scene = [MyGameLayer scene];
myGame = [MyGameLayer node];
myGame.delegate = self;
[scene addChild: myGame];
[[CCDirector sharedDirector] replaceScene:scene];
}
}
請注意,我們如何對待第一輪與第二輪不同。 對於第二次及以后的運行,我們將場景替換為新場景。 這樣可以避免所有“重新啟動”問題。 還要注意,我設置了一個代表。 我使用委托協議在游戲層和UIViewController之間進行通信。
我的單例模式來自Duck Rowing博客,我必須承認這是一個非常棒的博客名稱。 我不會在這里顯示所有單例代碼,此博客是關於cocos2d的,但是這是我構建cocos2d環境的方式。
+ (TTCocos2DSingleton *) sharedCocos2D;
{
static dispatch_once_t onceQueue;
dispatch_once(&onceQueue, ^{
if (sharedInstance) {
return;
}
sharedInstance = [[TTCocos2DSingleton alloc]init];
// Create an CCGLView with a RGB565 color buffer, and a depth buffer of 0-bits
sharedInstance->glView = [CCGLView viewWithFrame:[[UIScreen mainScreen] bounds]
pixelFormat:kEAGLColorFormatRGB565 //kEAGLColorFormatRGBA8
depthFormat:0 //GL_DEPTH_COMPONENT24_OES
preserveBackbuffer:NO
sharegroup:nil
multiSampling:NO
numberOfSamples:0];
[sharedInstance->glView setMultipleTouchEnabled:YES];
[sharedInstance setupDirector];
});
return sharedInstance;
}
單例設置CCGLView,啟用多點觸摸,然后設置控制器。 (我把它放在另一種方法中,因為我錯誤地認為我需要在其他地方調用它。事實證明,我並不需要。)
- (void)setupDirector
{
CCDirectorIOS *director = (CCDirectorIOS*) [CCDirector sharedDirector];
[director setView:glView];
[director enableRetinaDisplay:YES];
director.wantsFullScreenLayout = YES;
[director setDisplayStats:NO];
[director setAnimationInterval:1.0/60];
}
在setupDirector中,我們設置了cocos2d應用所需的常規可疑對象。 現在,該游戲可以播放多次,下面有一個完整的UIViewController / UINavController,並且在游戲頂部還有UIKit小部件。 涅槃。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.