iOS,UIImagePickerController,有没有办法检测用户何时点击拍照按钮而不实现自定义控件



Xcode 7.3, iOS 9.3.1

我想在用户即将拍照时和编辑图片后使用两个自定义叠加视图。 要更改叠加层,我想知道是否有来自UIImagePickerControllerDelegate的回调方法,该方法会在用户开始编辑图片或用户点击拍照按钮时让我知道。

我所知道的唯一方法是:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

我在这里看过: UIImagePicker控制器与相机OverlayView在点击叠加中的按钮时聚焦,如何知道我点击UIImagePickerer和iOS的"拍照按钮" - 在UIImagePickerController自定义覆盖视图中点击按钮后拍照。

或者,也许有一种方法可以通过添加观察器从标准按钮获取tap事件。

请帮忙! 提前谢谢。

您可以使用在使用UIImagePickerController时还必须实现的UINavigationControllerDelegate。 实现委托方法[...]didShowViewController[...]像这样

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSLog(@"%@", [viewController class]);
}

生成以下输出(使用模拟器,选择UIImagePickerControllerSourceTypePhotoLibrary作为sourceType并按如下所示导航:特定相册>相册概述>编辑特定照片(:

2016-04-08 00:19:36.882 图像选择器[44578:4705834] PUUIAlbumListViewController
2016-04-08 00:19:41.341 图像选择器[44578:4705834] PUUIMomentsGridViewController
2016-04-08 00:19:47.929 图像选择器[44578:4705834] PUUIImageViewController

希望对您有所帮助!

UIImagePickerControllerDelegate 很差,但你可以通过添加一个观察者来处理它:

斯威夫特 3:

NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "_UIImagePickerControllerUserDidCaptureItem"), object:nil, queue:nil, using: { note in
    //Do something
})

目标-C:

[[NSNotificationCenter defaultCenter] addObserverForName:@"_UIImagePickerControllerUserDidCaptureItem" object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
    //Do something
}];

Xcode 8.2.1, iOS10.2.1

在实施之前,请阅读早期版本的解决方案以了解基础知识。 谢谢! 问题是各种子视图的某些名称已从iOS 9.3.1更改为iOS 10.2.1。

这是完整的代码(请参阅下面的插入位置(,它替换了下面的"//代码转到此处":

for (UIView *subviewImagePickerControllerView in self.imagePickerController.view.subviews)
{
    if ([subviewImagePickerControllerView.class.description isEqualToString:@"UINavigationTransitionView"])
    {
        for (UIView *subviewUINavigationTransitionView in subviewImagePickerControllerView.subviews)
        {
            if ([subviewUINavigationTransitionView.class.description isEqualToString:@"UIViewControllerWrapperView"])
            {
                for (UIView *subviewUIViewControllerWrapperView in subviewUINavigationTransitionView.subviews)
                {
                    if ([subviewUIViewControllerWrapperView.class.description isEqualToString:@"CAMCameraViewControllerContainerView"])
                    {
                        for (UIView *subviewCAMCameraViewControllerContainerView in subviewUIViewControllerWrapperView.subviews)
                        {
                            if ([subviewCAMCameraViewControllerContainerView.class.description isEqualToString:@"CAMViewfinderView"])
                            {
                                for (UIView *subviewCAMViewfinderView in subviewCAMCameraViewControllerContainerView.subviews)
                                {
                                    if ([subviewCAMViewfinderView.class.description isEqualToString:@"CAMBottomBar"])
                                    {
                                        for (UIView *subviewCAMBottomBar in subviewCAMViewfinderView.subviews)
                                        {
                                            if ([subviewCAMBottomBar.class.description isEqualToString:@"CUShutterButton"] && [subviewCAMBottomBar.class isSubclassOfClass:[UIButton class]])
                                            {
                                                UIButton *shutterButton = (UIButton *)subviewCAMBottomBar;
                                                [shutterButton addTarget:self action:@selector(touchUpInsideCMKShutterButton) forControlEvents:UIControlEventTouchUpInside];
                                            }
                                            else
                                            {
                                                nil;
                                            }
                                        }
                                    }
                                }
                            }
                            else if ([subviewCAMCameraViewControllerContainerView.class.description isEqualToString:@"PLCropOverlay"])
                            {
                                for (UIView *subviewPLCropOverlay in subviewCAMCameraViewControllerContainerView.subviews)
                                {
                                    if ([subviewPLCropOverlay.class.description isEqualToString:@"PLCropOverlayBottomBar"])
                                    {
                                        for (UIView *subviewPLCropOverlayBottomBar in subviewPLCropOverlay.subviews)
                                        {
                                            if ([subviewPLCropOverlayBottomBar.class.description isEqualToString:@"PLCropOverlayPreviewBottomBar"])
                                            {
                                                for (UIView *itemPLCropOverlayPreviewBottomBar in subviewPLCropOverlayBottomBar.subviews)
                                                {
                                                    if ([itemPLCropOverlayPreviewBottomBar.class isSubclassOfClass:[UIButton class]])
                                                    {
                                                        UIButton *buttonPLCropOverlay = (UIButton *)itemPLCropOverlayPreviewBottomBar;
                                                        if ([buttonPLCropOverlay.titleLabel.text isEqualToString:@"Retake"])
                                                        {
                                                            UIButton *retakeButton = buttonPLCropOverlay;
                                                            [retakeButton addTarget:self action:@selector(touchUpInsideButtonRetake) forControlEvents:UIControlEventTouchUpInside];
                                                        }
                                                        else
                                                        {
                                                            nil;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        nil;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                nil;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        nil;
                                    }
                                }
                            }
                            else
                            {
                                nil;
                            }
                        }
                    }
                    else
                    {
                        nil;
                    }
                }
            }
            else
            {
                nil;
            }
        }
    }
    else
    {
        nil;
    }
}

Xcode 7.3, iOS9.3.1

我必须解决这个问题,所以我花了很多时间来解决这个问题。

它的要点是,我在呈现UIImagePickerController后向下钻取视图层次结构,寻找CMKShutterButton和标题为"重考"的重考按钮,然后我为按钮的操作附加一个选择器,就像这样......

[shutterButton addTarget:self action:@selector(touchUpInsideCMKShutterButton) forControlEvents:UIControlEventTouchUpInside];
[retakeButton addTarget:self action:@selector(touchUpInsideButtonRetake) forControlEvents:UIControlEventTouchUpInside];

将下面的代码拖放到显示图像选取器后调用的完成块中:

[self presentViewController:self.imagePickerController animated:true completion:^(void){
     //Code goes here
}

这是完整的代码,它取代了上面的"//代码去这里":

for (UIView *subviewImagePickerControllerView in self.imagePickerController.view.subviews)
{
    if ([subviewImagePickerControllerView.class.description isEqualToString:@"UINavigationTransitionView"])
    {
        for (UIView *subviewUINavigationTransitionView in subviewImagePickerControllerView.subviews)
        {
            if ([subviewUINavigationTransitionView.class.description isEqualToString:@"UIViewControllerWrapperView"])
            {
                for (UIView *subviewUIViewControllerWrapperView in subviewUINavigationTransitionView.subviews)
                {
                    if ([subviewUIViewControllerWrapperView.class.description isEqualToString:@"PLImagePickerCameraView"])
                    {
                        for (UIView *subviewPLImagePickerCameraView in subviewUIViewControllerWrapperView.subviews)
                        {
                            if ([subviewPLImagePickerCameraView.class.description isEqualToString:@"CMKBottomBar"])
                            {
                                for (UIView *itemCMKBottomBar in subviewPLImagePickerCameraView.subviews)
                                {
                                    if ([itemCMKBottomBar.class.description isEqualToString:@"CMKShutterButton"] && [itemCMKBottomBar.class isSubclassOfClass:[UIButton class]])
                                    {
                                        UIButton *shutterButton = (UIButton *)itemCMKBottomBar;
                                        [shutterButton addTarget:self action:@selector(touchUpInsideCMKShutterButton) forControlEvents:UIControlEventTouchUpInside];
                                    }
                                    else
                                    {
                                        nil;
                                    }
                                }
                            }
                            else if ([subviewPLImagePickerCameraView.class.description isEqualToString:@"PLCropOverlay"])
                            {
                                for (UIView *subviewPLCropOverlay in subviewPLImagePickerCameraView.subviews)
                                {
                                    if ([subviewPLCropOverlay.class.description isEqualToString:@"PLCropOverlayBottomBar"])
                                    {
                                        for (UIView *subviewPLCropOverlayBottomBar in subviewPLCropOverlay.subviews)
                                        {
                                            if ([subviewPLCropOverlayBottomBar.class.description isEqualToString:@"PLCropOverlayPreviewBottomBar"])
                                            {
                                                for (UIView *itemPLCropOverlayPreviewBottomBar in subviewPLCropOverlayBottomBar.subviews)
                                                {
                                                    if ([itemPLCropOverlayPreviewBottomBar.class isSubclassOfClass:[UIButton class]])
                                                    {
                                                        UIButton *buttonPLCropOverlay = (UIButton *)itemPLCropOverlayPreviewBottomBar;
                                                        if ([buttonPLCropOverlay.titleLabel.text isEqualToString:@"Retake"])
                                                        {
                                                            UIButton *retakeButton = buttonPLCropOverlay;
                                                            [retakeButton addTarget:self action:@selector(touchUpInsideButtonRetake) forControlEvents:UIControlEventTouchUpInside];
                                                        }
                                                        else
                                                        {
                                                            nil;
                                                        }
                                                    }
                                                    else
                                                    {
                                                        nil;
                                                    }
                                                }
                                            }
                                            else
                                            {
                                                nil;
                                            }
                                        }
                                    }
                                    else
                                    {
                                        nil;
                                    }
                                }
                            }
                            else
                            {
                                nil;
                            }
                        }
                    }
                    else
                    {
                        nil;
                    }
                }
            }
            else
            {
                nil;
            }
        }
    }
    else
    {
        nil;
    }
} 

这是我附加的方法,它位于呈现图像选择器控制器的视图控制器中:

- (void)touchUpInsideCMKShutterButton
{
    NSLog(@"Take");
}
- (void)touchUpInsideButtonRetake
{
    NSLog(@"Re-take");
}

希望这对某人有所帮助! 谢谢。

最新更新