4

我正在使用以下代码显示 UIImagePickerController 来拍照:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setDelegate:self];    
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagePicker animated:YES completion:nil];

注意:self 是嵌入在 Container View 中的 UIViewController,它本身参与了 UINavigationController

我还实现了以下方法:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];  
    [self.imgProfile setImage:image];   
    [self dismissViewControllerAnimated:YES completion:nil];    
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

当我选择拍摄的照片或取消它时,UIImagePickerController 消失了,但主视图也消失了!淡入黑色。

我正在仅 iPad 的应用程序上为 iOS7 编写代码,以防它与它有关。

这是一个显示问题的视频。原谅模糊,但它在 NDA 之下。

http://www.youtube.com/watch?v=sIaPyRlIqyE

4

7 回答 7

14

我在iOS7下遇到了同样的问题,我找到的唯一解决方案如下;

当从你的 VC 中展示选择器时;

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

[self addChildViewController:picker] ;

[picker didMoveToParentViewController:self] ;

[self.view addSubview:picker.view] ;

然后,在处理选择器代表时 - 而不是dismissModal,使用 ;

[picker.view removeFromSuperview] ;
[picker removeFromParentViewController] ;

你失去的只是动画的礼物/解雇——除此之外,一切都应该很好。

于 2013-11-25T11:49:31.507 回答
4

尝试将您的图像选择器设置.modalPresentationStyle.OverCurrentContext.OverFullScreen

交替:

viewController.definesPresentationContext = true
imagePicker.modalPresentationStyle = .CurrentContext OR .FullScreen

关键是这两个属性之间的关系需要这样保留:如果false那么.OverCurrentContext,如果true那么.CurrentContext

就我而言,我的视图控制器.definesPresentationContext默认为 false,所以我使用了第一个选项。

有关一些见解,请参阅文档。

于 2016-09-29T00:52:42.750 回答
3

您应该关闭选择器,而不是您的视图(自我),并确保添加这些:

<UINavigationControllerDelegate, UIImagePickerControllerDelegate>

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

UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;

[picker dismissViewControllerAnimated:YES completion:NULL];

}

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

[picker dismissViewControllerAnimated:YES completion:NULL];

}
于 2015-11-12T07:39:14.100 回答
1

当 UIImagePickerController 正式关闭时,它会在导航堆栈中紧随其后的任何 VC 上触发以下方法:

- viewWillAppear
- viewDidAppear
- viewDidLayoutSubviews
a bunch of other methods having to do with basic VC lifecycle.

presentViewController:就我而言,我并没有使用该方法展示最顶层的 VC 。我将它实例化为根 VC 的子级并做一些自定义动画。-viewDidLayoutSubviews我在该方法中完成了大部分工作。这项工作的一部分是指定我的子视图的框架处于屏幕外位置。

因此,当我将我的子视图动画到屏幕上,呈现相机,然后将其关闭时,该应用程序最终触发了-viewDidLayoutSubviews我的 rootVC,这将我的 childVC 移出了屏幕。

所以,我想答案是确保初始的 VC 设置代码只能被触发一次(使用类似于Bool setupHasAlreadyHappenedOnce在布置所有内容之前的东西)。

于 2014-08-07T17:42:22.543 回答
0

建议您在像这样将相机胶卷打开到 iPad 时使用 UIPopOver,因此请尝试一下,希望对您有所帮助。

#pragma mark
#pragma mark - Image Picker
-(IBAction)onClickTakePhoto:(id)sender
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {

        imagePickerController = [[UIImagePickerController alloc] init];        
        imagePickerController.delegate = self;
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePickerController.mediaTypes = [UIImagePickerController  availableMediaTypesForSourceType:imagePickerController.sourceType];
        imagePickerController.allowsEditing = YES;

        if ([self respondsToSelector:@selector(presentViewController:animated:completion:)]) {
            [self presentViewController:imagePickerController animated:YES completion:nil];
        } else {
            [self presentModalViewController:imagePickerController animated:YES];
        }        
        [imagePickerController release];
    }
}

-(IBAction)onClickChooseFromLibrary:(id)sender
{
    if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
    {
        imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.mediaTypes = [UIImagePickerController  availableMediaTypesForSourceType:imagePickerController.sourceType];
        popoverController= [[UIPopoverController alloc]
                        initWithContentViewController:imagePickerController];
        [popoverController presentPopoverFromRect:btnLibrary.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
    }
}

#pragma mark
#pragma mark - UIImagePickerDelegate
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{    
    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)])   
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self dismissModalViewControllerAnimated:YES];
    }
    [popoverController dismissPopoverAnimated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{    
    if ([self respondsToSelector:@selector(dismissViewControllerAnimated:completion:)]) 
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    } else {
        [self dismissModalViewControllerAnimated:YES];
    }

    [popoverController dismissPopoverAnimated:YES];
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    [userImage setLocalImage:image];
}
于 2013-10-03T04:13:25.887 回答
0

请注意,UIImagePickerController 的委托是“双重委托”,它也委托 UINavigationControllerDelegate。请参阅下面的代码。

@property(nullable,nonatomic,weak) id <UINavigationControllerDelegate, UIImagePickerControllerDelegate> delegate;

所以,检查是否self也实现UINavigationControllerDelegate,并像下面那样关闭父视图控制器。

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
    NSMutableArray *stack = [navigationController.viewControllers mutableCopy];
    if (stack.count >= 2) {
        [stack removeObjectAtIndex:(stack.count-2)];
    }
    navigationController.viewControllers = [stack copy];
    navigationController.delegate = nil;
}

好的,我做到了,我花了很多时间来找出答案。我的情况可能并不常见。这仅供参考。

于 2016-09-21T08:59:30.600 回答
0

当我尝试从 UIContainerView 中呈现 UIImagePickerController 时,我遇到了类似但不完全的问题。

问题是解除 UIImagePickerController 改变了 UIContainerView 中的导航(调整为全屏)。

解决方案是从 UIContainerView 的父视图控制器中呈现 UIImagePickerController ,现在一切正常。

于 2016-02-19T10:57:06.177 回答