8

Im trying to set up an imagePicker in a new iOS7 Xcode project using storyboards but can't seem to find any examples online until I found the following code.

I've set it up so that when a button is pressed it uses a modal push thing to navigate to the view which has the class of "UIImagePickerController" which apparently calls in the image picker code.

However when the app is run it won't load anything up after clicking on allow the application to access your photos any chance of some help please?

Error screen shot grab: https://pbs.twimg.com/media/Bdy7v9JCAAABpvS.jpg:large

interface setup:

@interface ridecount_AddRide_ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{

code:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    UIImagePickerController *controller = [segue destinationViewController];
    controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    controller.delegate = self;
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [self dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [[self.view viewWithTag:100023] removeFromSuperview];
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(320/2-200/2, 10, 100, 100)];
    imageView.tag = 100023;
    imageView.contentMode = UIViewContentModeScaleAspectFit;
    imageView.image = image;
    [self.view addSubview:imageView];
}
Prince Agrawal
  • 3,619
  • 3
  • 26
  • 41
user3187832
  • 103
  • 4

4 Answers4

8

Initialize the controller UIImagePickerController *controller = [[segue destinationViewController] init];

Matt
  • 2,329
  • 1
  • 21
  • 23
  • 1
    Why isn't the storyboard init'ing the view controller ? Isn't that strange behavior? But thanks! It works :) – Avba Aug 07 '14 at 09:29
  • thx, your [[segue destinationViewController] init] approach saves me from using an ugly void segue workaround – Jakob Jan 14 '15 at 16:46
  • @AvnerBarr the true is that the storyboard initialize `destinationViewController` with `initWithCoder:` method. When the `UIImagePickerController` is initialized programatically (not from storyboard) with the `init` method, it probably makes some additional stuff comparing to `initWithCoder:` method. That is why it is not fully working using storyboard. I don't recommend performing `init...` method one more time in `prepareForSegue` body. The object shouldn't be initialized more than once. Please check my answer with the alternative solution. – kowal Mar 20 '15 at 11:53
3

for SWIFT - bit different approach

//Init property
var imagePicker = UIImagePickerController()

then assign

override func prepareForSegue(segue:UIStoryboardSegue!, sender: AnyObject!)
{
    imagePicker = segue.destinationViewController as UIImagePickerController
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false
    imagePicker.delegate = self
}
Vanya
  • 4,973
  • 5
  • 32
  • 57
0

I had the same problem. The UIImagePickerController object which was initialized from the storyboard worked correctly only when the availableMediaTypesForSourceType was set to UIImagePickerControllerSourceTypeCamera.

If you want to preserve prepareForSegue: functionality you can remove the UIImagePickerController from the storyboard and create it programatically using the following code:

- (void)showCameraRollController
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    UIStoryboardSegue *segue = [UIStoryboardSegue segueWithIdentifier:@"CameraRollController" source:self destination:imagePickerController performHandler:^{
        [self presentViewController:imagePickerController animated:YES completion:NULL];
    }];
    [self prepareForSegue:segue sender:self];
    [segue perform];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"CameraRollController"]) {
        UIImagePickerController *imagePickerController = [segue destinationViewController];
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePickerController.delegate = self;
    }
}
kowal
  • 899
  • 8
  • 9
0

Possibly, another alternative is to subclass UIImagePickerController and add to the subclass:

- (instancetype)initWithCoder:(NSCoder *)aDecoder { return [super init]; }

It seemed to work.

Paul Gardiner
  • 468
  • 2
  • 7