简体   繁体   English

在iOS8中使用ImagePicker Controller捕获后,图像向下移动

[英]Image moves down after capturing using ImagePicker Controller in iOS8

I have placed an overlay image over camera view and trying to Capture the image but as soon as I capture the image (I have not tapped the "Use Photo" button yet), the captured image moves some pixel down and the height of top black bar having flash and front/back camera settings buttons suddenly increases. 我在相机视图上放置了一个叠加图像并尝试捕获图像但是一旦我捕获图像(我还没有点击“使用照片”按钮),捕获的图像会向下移动一些像素并且顶部黑色的高度闪光灯和前/后相机设置按钮突然增加。 So the situation now is - my overlay image has not moved a single pixel and the captured image has moved downwards. 所以现在的情况是 - 我的叠加图像没有移动单个像素,并且捕获的图像向下移动。 So I am not able to capture the correct image with overlay placed properly. 因此,我无法在正确放置叠加层的情况下捕获正确的图像。 This issue is only in iOS8 and above. 此问题仅适用于iOS8及更高版本。

Anybody facing this issue on iOS8 ?? 在iOS8上遇到这个问题的人?

It turns out that for the iPhone (when the UImagePickerController is presented under a navigation controller) the navigation bar is hidden when you take a picture. 事实证明,对于iPhone(当UImagePickerController在导航控制器下显示时),当您拍照时,导航栏会被隐藏。 In the following preview, however, the navigation bar is no longer hidden even if it's not visible and unhiding moves the preview image down or to the right, depending on the orientation. 但是,在以下预览中,即使导航栏不可见,也不再隐藏导航栏,并且取消隐藏会根据方向向下或向右移动预览图像。 To compensate for this change you'll have to adjust the origin of the camera overlay view accordingly. 要补偿此更改,您必须相应地调整相机叠加视图的原点。 I resolved this by adding a callback that handles the UIImagePickerController notifications. 我通过添加一个处理UIImagePickerController通知的回调来解决这个问题。 In your code where you initiate the UIImagePickerController add the following code: 在您启动UIImagePickerController的代码中添加以下代码:

        [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleImagePickerNotification:)
                                                 name:@"_UIImagePickerControllerUserDidCaptureItem"
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleImagePickerNotification:)
                                                 name:@"_UIImagePickerControllerUserDidRejectItem"
                                               object:nil];

The callback which handles the notification must distinguish between the UserDidCaptureItem and UserDidRejectItem notifications. 处理通知的回调必须区分UserDidCaptureItem和UserDidRejectItem通知。 In the first case the x- or y-origin of the camera overlay frame is incremented (depending on the orientation), in the second case the increment is reverted again. 在第一种情况下,相机覆盖帧的x或y原点递增(取决于方向),在第二种情况下,再次恢复增量。 The size of the increment or decrement is the height of the navigation bar. 增量或减量的大小是导航栏的高度。

- (void)handleImagePickerNotification:(NSNotification *)notification {

// This notification compensates for a feature (?) introduced with iOS 8 that moves the
// image after it's been taken with the UIImagePickerController. 

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

    CGRect frame = cameraOverlay.frame;
    UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
    CGFloat barHeight = [self.navigationController navigationBar].frame.size.height;

    if ([[notification name] isEqualToString:@"_UIImagePickerControllerUserDidCaptureItem"]) {
        if (UIInterfaceOrientationIsLandscape(orientation))
            frame.origin.x += barHeight;
        else
            frame.origin.y += barHeight;
    } else
        if ([[notification name] isEqualToString:@"_UIImagePickerControllerUserDidRejectItem"]) {
            if (UIInterfaceOrientationIsLandscape(orientation))
                frame.origin.x -= barHeight;
            else
                frame.origin.y -= barHeight;
            [cameraOverlay setFrame:frame];
        }
    [cameraOverlay setFrame:frame];
}

This isn't a very elegant way of resolving the issue but in my case it worked. 这不是解决问题的一种非常优雅的方式,但在我的情况下它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM