UIImagePickerController:切换源类型不起作用

时间:2018-04-07 04:35:36

标签: ios objective-c uiimagepickercontroller

我有一个带有自定义叠加视图的UIImagePickerController。视图控制器最初以相机作为源类型打开。但是有一个按钮可以将源类型切换到相册。如果他们进入相册模式,他们可以点击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;
    }
}

1 个答案:

答案 0 :(得分:0)

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

override func dragStateDidChange(_ dragState: UICollectionViewCellDragState) {

    switch dragState {

    case .none:
        //
    case .lifting:
        //
    case .dragging:
        //
    }
}