PDA

View Full Version : iPad Image Selection Problems




CosmicChild87
Nov 4, 2010, 11:13 AM
Hi everyone,

Im a complete N00b when it comes to iPhone/iPad programming and im trying to play with a program from tutorials on the net, and im currently trying to get an image picker to work

I have a button that launches an image picker, in a sub view of my main view and this works well - but I can't seem to get the code to recognise when a user has selected an image.

I have tried the following

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

}

I don't get any errors when it runs - it just does nothing

Apploiges if this is an obvious answer but any help or any pointers in the right direction would be very very appreciated

Alan



jvaneat
Nov 4, 2010, 11:19 AM
You need to make sure that you have set the current view controller as an image picker delegate.

If your using Interface Builder you can do it there. Otherwise, when you create the image picker do:


UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;


And make sure the view controller can be a delegate. Open {Your}ViewController.h and add the UiImagePickerControllerDelegate portion like so:


@interface {Your}ViewController : UIViewController <UIImagePickerControllerDelegate>

CosmicChild87
Nov 4, 2010, 11:27 AM
Thanks, I have both those things done still no joy :( - probably should have added it before, but here is the code i use to implement popover/image picker

-(IBAction)logoLoad:(id)sender{
NSLog(@"Running Image Picker Code");
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
UIPopoverController *pop = [[UIPopoverController alloc] initWithContentViewController:picker];
picker.delegate = self;

pop.delegate = self;
CGRect popoverRect = [self.view convertRect:[sender frame] fromView:[sender superview]];

NSLog(@"Rect Selected");
[pop presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

NSLog(@"Rect Made and presented");
[pop setPopoverContentSize:CGSizeMake(200.0, 200.0)];
[picker release];

}

jvaneat
Nov 4, 2010, 11:43 AM
Have you verified that the didFinishPickingMediaWithInfo method is getting called?

If it is, try changing this:


[picker dismissModalViewControllerAnimated:YES];
theimageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];


To this:


theimageView.image = [info valueForKey:UIImagePickerControllerOriginalImage];
[picker dismissModalViewControllerAnimated:YES];


UIImagePickerControllerOriginalImage is defined as a string constant.

CosmicChild87
Nov 4, 2010, 11:58 AM
I had put an NSLog in as the first line, and it isnt being called at all :S


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"Running after picking");
theimageView.image = [info valueForKey:UIImagePickerControllerOriginalImage];
[picker dismissModalViewControllerAnimated:YES];

}


Does nothing and outputs nothing to the debugger :(

jvaneat
Nov 4, 2010, 12:01 PM
In logoLoad try getting ride of [picker release];

CosmicChild87
Nov 4, 2010, 12:21 PM
Fantastic thank you!, how would i go about getting the popover to close once the image is selected?

Alan

It's ok i was being stupid again - thank you so much for your help!!!!

jvaneat
Nov 4, 2010, 12:31 PM
When you define pop, do this:


pop.tag = 200; // Use a number you like, or better still define a constant



Then when you have found the image simply add this to didFinishPickingMediaWithInfo:


UIPopoverController *pop = (UIPopoverController *)[self.view viewWithTag:200];
[pop removeFromSuperView];
[pop release];