Monday, 30 January 2012
Wednesday, 18 January 2012
UIImagePickerController
This subject seems to be a common question on the Cocos2d forum, so being a good guy and wanting to contribute I have decided to share this code with forum members.
I’ve been working on a project for the iPhone that uses an image from the device. You can either get your images direct from the camera, photo albums or one of your saved images.
After spending some time looking through sample code and reading endless pages of information I was finally able to put some code together that works.
So here is the code:
First you need to add your Delegates to your Scene header like this in your .h file.
First you need to add your Delegates to your Scene header like this in your .h file.
@interface MenuScene : CCLayer <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIWindow *window;
UIImage *newImage;
}
Then you can use the UIImagePickerController in your .m file with the following code.
-(void)pickPhoto:(UIImagePickerControllerSourceType)sourceType{
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.delegate = self;
picker.sourceType = sourceType;
picker.wantsFullScreenLayout = YES;
[picker presentModalViewController:picker animated:YES];
[[[CCDirector sharedDirector] openGLView] addSubview:picker.view];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info{
// newImage is a UIImage do not try to use a UIImageView
newImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
// Dismiss UIImagePickerController and release it
[picker dismissModalViewControllerAnimated:YES];
[picker.view removeFromSuperview];
[picker release];
// Let's create a sprite now that we have an image
CCSprite *imageFromPicker = [CCSprite spriteWithCGImage:newImage.CGImage key:@"ImageFromPicker"];
}
Subscribe to:
Posts (Atom)