Using UIImagePickerController in the game - iphone

I want to make a game, where the user can touch a picture of a TV and then choose a picture from their photo library.
I had success working with UITextFields, adding them to my EAGLView as subviews But I haven't been able to do the same thing with an UIImagePickerController , which seems the only way to do what I want to do.
Any help is appreciated !
UPDATE
With the suggestion of zpasternack I managed to make the menu appear by adding the UIImagePickerControllerDelegate, UINavigationControllerDelegate and using this method:
- (void) chooseImageFromLibrary {
if( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] ) return;
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.allowsEditing = YES;
[self addSubview:imagePickerController.view];
[imagePickerController viewWillAppear:YES];
[imagePickerController viewDidAppear:YES];
}
I call the chooseImageFromLibrary method at the start. It shows the interface, but as soon as i hit cancel or choose the app stops.
Can anyone suggest me:
1) How to make the cancel and choose buttons work.
2) How to call the UIImagePickerController whenever I want ( by calling chooseImageFromLibrary again ??).
3) When the user hits choose..Where do i find the UIImage the user just picked to ?, how do i pick it ?
Thanks.

I've never used one over an EAGLView, but UIImagePickerControllers are typically invoked with presentModalViewController, like:
- (void) chooseImageFromLibrary {
if( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] ) return;
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerController.allowsEditing = YES;
[self presentModalViewController:imagePickerController animated:YES];
}

Nervermind, problem solved. i just needed to overwrite some functions to make use of the UIImagePickerController.

Related

Why does UIImagePickerControllerSourceTypePhotoLibrary not let the user pick an album?

I am currently developing an iPad application for iPad, where I use a UIPopOverController in combination with UIImagePickerController to let the user select an image from their iPad (with sourceType UIImagePickerControllerSourceTypeSavedPhotosAlbum). This works great and as expected, but it only allows the client to browse the SavedPhotos album obviously. If I change the picker to use UIImagePickerControllerSourceTypePhotoLibrary, the window will still display its albums, but it'll be static (meaning I can scroll, but I can't select albums, and when I scroll down or up, the view will only pop back if I tap the screen again).
Does the PhotoLibrary require extra settings to work properly? I tried numerous approaches but I'm all out of luck here.
The setup is an OpenGL view, on which I spawn the UIPopOverController with the UIImagePicker. For the sake of clarity, here's the code:
-(void)showPicker
{
if(![pController isPopoverVisible])
{
[pController presentPopoverFromRect:CGRectMake(50,100,200,300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionLeft animated:YES];
}
else
{
pController dismissPopoverAnimated:YES];
}
}
-(void)awakeFromNib
{
picker = [[UIImagePickerController alloc] init];
pController = [[UIPopoverController alloc] initWithContentViewController:picker];
// Code works properly if I only set it to UIImagePickerControllerSourceTypeSavedPhotosAlbum
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
else if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) picker.sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
else { NSLog(#"Error: Could not pick appropriate sourceType"); }
picker.mediaTypes = [NSArray arrayWithObjects:(NSString*)kUTTypeImage, nil];
picker.delegate = self;
pController.delegate = self;
picker.allowsEditing=NO;
}
Are you working with iOS6 ?
I know that in Settings/Privacy you have to allow your app to see in the Photos Albums.

Best way to use imagepickercontroller in iphone?

I am using UIImagePickerController to select the image from the PhotoLibrary in my application. I have used two different approaches for this. At first I have used a class variouble UIImagePicker with below code.
imagepicker = [[UIImagePickerController alloc]init];
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagepicker.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:self.imagepicker animated:YES];
Above code is working fine.But when I clicked on the button it is taking some time to react with the animation in this case.Then I used the autorelease pool approach with this method
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
if([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *picker= [[[UIImagePickerController alloc]init]autorelease];
picker.delegate = self;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.modalTransitionStyle = UIModalTransitionStylePartialCurl;
[self presentModalViewController:picker animated:YES];
}
[pool release];
Also works charm. Both of them showing no leak in the analyser.Can anybody point me the right approach.
Well, no much to say here... Both approaches work, both approaches are correct, use whichever you prefer.
One minor point: if you are regularly presenting the image picker, you better use the first method, and assign it to an instance variable (it isn't called a "class variable"!) only for the first time, and don't release it until - dealloc - this way, you save the continuous allocation-deallocation of the image picker every single time the user chooses an image.

Starting an iPhone app in camera mode shows just a blank screen

I'm creating an app for the iPhone which is supposed to start in camera mode (with a custom layout on top but that comes later). I've already created a version of this app where I press a button, that allows me to either choose a photo from the iPhone album or take a new photo.
Trying to use the same code in a different fails.
Originally I had a function 'takePicture' which I used to start the camera
- (void)takePicture
{
isInCaptureMode = YES;
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
imagePicker.videoQuality = UIImagePickerControllerQualityTypeMedium;
// set delegate
[imagePicker setDelegate:self];
// Place image picker on the screen
[self presentModalViewController:imagePicker animated:YES];
[imagePicker release];
}
So I thought to myself "I can probably create a single view app, throw this code into the view controller's viewDidLoad function and use that as a starting point". But alas, I've been stuck there. When I do that the application starts and (if the device has a camera) the screen goes blackish blank, if the device does not have a camera (iPad or the simulator) the app crashes.
I originally created the app using Xcode 3 but have now migrated over to 4.2 the original app runs fine in 4.2 though (after I did a little bit of tweaking)
Can anyone explain why this happens?
can you try the following code to see what happen?
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
//you can get picture from Library is there is no camera
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}
picker.delegate = self;
[self presentModalViewController:picker animated:YES];
[picker release];
So, like I said in the comment. As soon as I called the code from 'viewDidAppear' instead of 'viewDidLoad' it finally ran...

how to close imagePickerController?

I just can't get this:
how in the world can I "shut down" imagePickerController on xcode?
This is the code that opens the camera, and it works perfectly..
-(IBAction) startcamera {
imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.allowsEditing = NO;
imagePickerController.showsCameraControls=NO;
imagePickerController.toolbarHidden=YES ;
imagePickerController.wantsFullScreenLayout=YES;
imagePickerController.cameraOverlayView=self.view;
self.imagePickerController.delegate=self;
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
Ok now, say I want to close imagePickerController by clicking a button (means without picking any image): how am I supposed to do that?
Tried with:
-(IBAction)closecamera {
[imagePickerController release];
[imagePickerController dismissModalViewControllerAnimated:YES];
}
but it doesn't work!
Any clues?
Thanks in advance
Don't release imagePickerController twice.
You would dismiss it with [self dismissModalViewControllerAnimated:YES];
Don't release it twice and dismiss it before you release it. (like ole said)
But this is also important... as soon as you have released an object it is gone and no methods can be called.

iPhone dev noob question: Invoking action explicitly on application launch

I just started getting into iPhone development. I have been mish-mashing tutorials and material from books in order to get my bearings straight. I come from a PHP and Java background... Objective-C is a bit quirky. But, I learn best by getting my feet wet.
Basically, I have these actions. getPhoto is bound to a couple of UIBarButtonItems in my view.
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIBarButtonItem *) sender == choosePhoto) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
theimageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
My goal is to invoke the same action once the application launches, automatically opening the Camera. How would I go about this?
EDIT:
As per this SO question you should actually place it in viewWillAppear or viewDidAppear
Add a similar method to the ApplicationDidFinishLaunching method in the app delegate.
Might be better to place the call in the ViewDidLoad of your root view controller