简体   繁体   中英

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.

Anybody facing this issue on 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. 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. In your code where you initiate the UIImagePickerController add the following code:

        [[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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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