iOS7 Storyboard image picker not working :(

佐手、 提交于 2019-11-28 07:32:45

问题


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

回答1:


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




回答2:


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
}



回答3:


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



回答4:


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

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

It seemed to work.



来源:https://stackoverflow.com/questions/21145270/ios7-storyboard-image-picker-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!