Pulling up an Image Picker with ITGB
by Ecliptic · in iTorque 2D · 07/08/2009 (10:21 am) · 2 replies
I have been saying for awhile that I was going to post my image picker code and files for the community so today I wanted to set some time aside to get it out there for you to start messing with. I have been short on time and wanted to have this code optimized and cleaned before posting because a lot of it was figured out through trial and error. That being said...
DISCLAIMER
**** THIS IS PROBABLY NOT THE ONLY WAY TO GET THE IMAGEPICKER AND THERE ARE MULTIPlE CORRECTIONS THAT COULD BE MADE TO THE CODE. I AM NOT AN VERY EXPERIENCED PROGRAMMER SO IF THERE ARE MISTAKES I APOLOGISE.***
Ahh.. Now that is out of the way lets begin. This first file is the header file for the Image picker. I placed it in the class folder folder along with the Textentry stuff that Michael was working on. You could place this anywhere you like as long as you make sure to include it with the right path.
Next you will need to create your cpp file that will contain the information on how the Image picker works. In this file you can see the line that saves out the image with a name of "SavedImage". You can change that to whatever you like obviously. The way I set up my image picker was to save a file one time and erase over itself since I didn't want to make a lot of uneccessary files on the users device. You will have to modify this if you want it to save multiple images to be used at all times.
Also, if you want to save a JPEG instead of a PNG then you will need to change this line of code.
Ok next you are going to need to create a set of wrappers. This is the .mm file that I used.
This is the header fle, as you can tell there is not much to it as I haven't had time to finish everything but this should work.
In my next post I will setup a download where you guys can get the XIB file for this as it will be needed to pull up the image pickers interface. You will want to put this XIB file in a folder called UI and place it into your resources with the second option clicked. This will insure that it is installed on the device with your game to be used.
I hope this helps some of you get started with the image picker and can expand on this with a much better version. I will be working on this some more in the future but for now I have so much work to catch up on that I haven't had any time to devote to this. If there are any questions I will try to help.
Also give a special thanks to Mat Valadez and Michael Perry for their help on getting this to this point. I had nagged them both several times throughout the process :).
-Ecliptic
Here are all the files needed. Good luck and post any things you add please for other users!
PhotoLibrary_Torque.zip
DISCLAIMER
**** THIS IS PROBABLY NOT THE ONLY WAY TO GET THE IMAGEPICKER AND THERE ARE MULTIPlE CORRECTIONS THAT COULD BE MADE TO THE CODE. I AM NOT AN VERY EXPERIENCED PROGRAMMER SO IF THERE ARE MISTAKES I APOLOGISE.***
Ahh.. Now that is out of the way lets begin. This first file is the header file for the Image picker. I placed it in the class folder folder along with the Textentry stuff that Michael was working on. You could place this anywhere you like as long as you make sure to include it with the right path.
#import <UIKit/UIKit.h>
class SelectedPhoto;
@interface ImagePickerViewController : UIViewController <UINavigationControllerDelegate,UIImagePickerControllerDelegate> {
IBOutlet UIButton *button;
IBOutlet UIImageView *ImageView;
bool finished;
bool userCanceled;
}
@property(nonatomic, readonly) IBOutlet UIImageView *ImageView;
@property(nonatomic, readonly) bool finished;
@property(nonatomic, readonly) bool userCanceled;
- (IBAction)openPicker;
- (IBAction)onCancelClicked: (id)sender;
- (IBAction)onCommitClicked: (id)sender;
-(void)useImage:(UIImage *)theImage;
@endNext you will need to create your cpp file that will contain the information on how the Image picker works. In this file you can see the line that saves out the image with a name of "SavedImage". You can change that to whatever you like obviously. The way I set up my image picker was to save a file one time and erase over itself since I didn't want to make a lot of uneccessary files on the users device. You will have to modify this if you want it to save multiple images to be used at all times.
#import "ImagePickerViewController.h"
@implementation ImagePickerViewController
@synthesize finished;
@synthesize userCanceled, ImageView;
- (IBAction)openPicker {
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker;
picker = [[UIImagePickerController alloc] init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsImageEditing = YES;
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
}
}
-(void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo {
[self useImage:image];
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[[picker parentViewController] dismissModalViewControllerAnimated:YES];
}
-(void)useImage:(UIImage *)theImage {
ImageView.image = theImage;
}
- (IBAction)onCancelClicked: (id)sender
{
userCanceled = true;
finished = true;
[self.view removeFromSuperview];
}
- (IBAction)onCommitClicked: (id)sender
{
finished = true;
[self.view removeFromSuperview];
NSData *imageData = UIImagePNGRepresentation(ImageView.image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"SavedImage.png"];
[imageData writeToFile:documentsDirectory atomically:YES];
}
@endAlso, if you want to save a JPEG instead of a PNG then you will need to change this line of code.
NSData *imageData = UIImagePNGRepresentation(ImageView.image); to NSData *imageData = UIImageJPEGRepresentation(ImageView.image, 1.0);
Ok next you are going to need to create a set of wrappers. This is the .mm file that I used.
#import <Foundation/Foundation.h>
#include "ImagePicker.h"
#include "../Classes/ImagePickerViewController.h"
void PhotoLibrary::getUserPhoto()
{
ImagePickerViewController* controller = [[ImagePickerViewController alloc] initWithNibName: @"ImagePickerViewController" bundle: nil];
UIWindow* window = [UIApplication sharedApplication].keyWindow;
[controller loadView];
controller.view.alpha = 0.0f;
[window addSubview: controller.view];
[UIView beginAnimations: nil context: nil];
controller.view.alpha = 1.0f;
[UIView commitAnimations];
bool finished = controller.finished;
if (!finished)
{
//Need to return something here?
}
}
@endThis is the header fle, as you can tell there is not much to it as I haven't had time to finish everything but this should work.
#ifndef IMAGEPICKER_INCLUDED
#define IMAGEPICKER_INCLUDED
#include <core/stringBuffer.h>
namespace PhotoLibrary
{
void getUserPhoto();
}
#endifIn my next post I will setup a download where you guys can get the XIB file for this as it will be needed to pull up the image pickers interface. You will want to put this XIB file in a folder called UI and place it into your resources with the second option clicked. This will insure that it is installed on the device with your game to be used.
I hope this helps some of you get started with the image picker and can expand on this with a much better version. I will be working on this some more in the future but for now I have so much work to catch up on that I haven't had any time to devote to this. If there are any questions I will try to help.
Also give a special thanks to Mat Valadez and Michael Perry for their help on getting this to this point. I had nagged them both several times throughout the process :).
-Ecliptic
Here are all the files needed. Good luck and post any things you add please for other users!
PhotoLibrary_Torque.zip
#2
I will spend this weekend trying out your code.
03/31/2010 (12:13 am)
Hey Ecliptic. This is awesome. In my app I want to take a picture and use it as a background. Is that possible with your code?I will spend this weekend trying out your code.
Torque Owner Ecliptic
ConsoleFunction(getPhoto, bool, 1, 1, "Open the Phot Viewer" ) { PhotoLibrary::getUserPhoto (); }After doing this you should be able to use getPhoto() to open up the image picker. Also those two wrapper files can be anywhere you like them to be, for me I just created a new folder called wrappers.
-Ecliptic