UIImagePickerController:切换源类型不起作用

UIImagePickerController: switching source types does not work

我有一个带有自定义覆盖视图的 UIImagePickerController。视图控制器最初以相机作为源打开 type.But 有一个按钮可以将源类型切换为相册。如果他们进入相册模式,他们可以点击 cancel 将源类型切换回相机。

这适用于第一轮。但是如果他们第二次按下相册按钮(在已经进入相册模式并取消之后),屏幕会加载白色背景但不会加载用户的照片库。然后应用程序卡住了。

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
        ipc.showsCameraControls = NO;

        [[NSBundle mainBundle] loadNibNamed:@"CameraView" owner:self options:nil];
        self.overlayView.frame = ipc.cameraOverlayView.frame;
        ipc.cameraOverlayView = self.overlayView;
        self.overlayView = nil;
        CGSize screenBounds = [UIScreen mainScreen].bounds.size;
        int cameraViewHeight;
        int adjustedYPosition;

        /*Center Uiimagepickercontroller in screen */
            if (UIDeviceOrientationIsPortrait([[UIDevice currentDevice] orientation])) {
                cameraViewHeight = screenBounds.width * 1.333;
                adjustedYPosition = (screenBounds.height - cameraViewHeight) / 2;
                NSLog(@"portrait screenbounds width: %f, screenbounds height: %f", screenBounds.width, screenBounds.height);
                CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
                ipc.cameraViewTransform = translate;
                NSLog(@"in portrait value is %d", adjustedYPosition);
            }else{
            //landscape mode
                cameraViewHeight = screenBounds.height * 1.333;
                adjustedYPosition = (screenBounds.width - cameraViewHeight) / 2;
                CGAffineTransform translate = CGAffineTransformMakeTranslation(0, adjustedYPosition);
                ipc.cameraViewTransform = translate;
            }

        ipc.showsCameraControls = NO;
        ipc.tabBarController.tabBar.hidden = YES;
    }else{
        ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
    [self presentViewController:ipc animated:YES completion:nil];
    [_activityIndicator stopAnimating];
}

- (IBAction)albumView:(id)sender {
    [ipc setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{


    /*navigate to previous tab */
    if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
        [picker dismissViewControllerAnimated:YES completion:nil];
        LeafsnapTabBarController * tabBarController = (LeafsnapTabBarController*)self.tabBarController;
        [self.tabBarController setSelectedIndex:tabBarController.previousTabIndex];
    }else{
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
}

我不确定为什么会这样,所以如果有人能解释一下,我会将他们的回复标记为正确。但这是我的解决方案:

- (IBAction)albumView:(id)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    [imagePicker.view setFrame:self.view.frame];
    [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    [imagePicker setDelegate:self];

    [ipc presentViewController:imagePicker animated:YES completion:nil];
}

- (IBAction)cancel_IPC:(id)sender {
    [self imagePickerControllerDidCancel:ipc];

}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    [picker dismissViewControllerAnimated:YES completion:nil];
    if(picker.sourceType == UIImagePickerControllerSourceTypeCamera){
        LeafsnapTabBarController * tabBarController = (LeafsnapTabBarController*)self.tabBarController;
        [self.tabBarController setSelectedIndex:tabBarController.previousTabIndex];
    }

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage* origImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    /*do whatever you need to do with the img*/

    [picker dismissViewControllerAnimated:YES completion:nil];
    /*dismiss the original UIImagePickerController in background */
    if(picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary){
        [ipc dismissViewControllerAnimated:YES completion:nil];
    }

    [mLocationManager stopUpdatingLocation];

}