iOS7 Storyboard image picker not working :(
Asked Answered
T

4

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];
}
Twotone answered 15/1, 2014 at 18:19 Comment(0)
M
8

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

Memorialize answered 15/1, 2014 at 18:39 Comment(3)
Why isn't the storyboard init'ing the view controller ? Isn't that strange behavior? But thanks! It works :)Cognizant
thx, your [[segue destinationViewController] init] approach saves me from using an ugly void segue workaroundKazan
@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.Felicitous
F
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
}
Flurried answered 16/8, 2014 at 19:3 Comment(0)
F
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;
    }
}
Felicitous answered 20/3, 2015 at 12:5 Comment(0)
R
0

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

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

It seemed to work.

Refractive answered 29/10, 2015 at 15:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.