简体   繁体   中英

iOS7 Storyboard image picker not working :(

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];
}

初始化控制器UIImagePickerController *controller = [[segue destinationViewController] init];

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
}

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;
    }
}

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

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

It seemed to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM