我知道我需要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
。
请在这个问题上帮助我,提供代码片段或有用的教程,或者给我你的黄金建议。