iOS7故事板图像选择器无法正常工作:(

时间:2014-01-15 18:19:35

标签: ios objective-c image storyboard segue

我尝试使用故事板在新的iOS7 Xcode项目中设置imagePicker,但在找到以下代码之前似乎无法在线找到任何示例。

我已经设置了它,以便当按下按钮时,它使用模态推送物体导航到具有"UIImagePickerController"类的视图,该类显然在图像选择器代码中调用。

然而,当应用程序运行时,点击后允许应用程序访问您的照片,任何获得帮助的机会都不会加载任何内容吗?

错误截屏: https://pbs.twimg.com/media/Bdy7v9JCAAABpvS.jpg:large

界面设置:

@interface ridecount_AddRide_ViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate>{

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

4 个答案:

答案 0 :(得分:8)

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

答案 1 :(得分:2)

用于SWIFT - 有点不同的方法

//Init property
var imagePicker = UIImagePickerController()

然后分配

override func prepareForSegue(segue:UIStoryboardSegue!, sender: AnyObject!)
{
    imagePicker = segue.destinationViewController as UIImagePickerController
    imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    imagePicker.allowsEditing = false
    imagePicker.delegate = self
}

答案 2 :(得分:0)

我遇到了同样的问题。仅当UIImagePickerController设置为availableMediaTypesForSourceType时,从故事板初始化的UIImagePickerControllerSourceTypeCamera对象才能正常工作。

如果您想保留prepareForSegue:功能,可以从故事板中删除UIImagePickerController,然后使用以下代码以编程方式创建它:

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

答案 3 :(得分:0)

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

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

It seemed to work.