26

我在与 iOS6 完美配合的 UIPopoverController 中使用 UIImagePickerController。在 iOS 7 中,为捕捉图像而显示的“预览”图像被旋转,但如果我拍照,它会被正确保存。

这就是我得到我的选择器的方式:

UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
                              (NSString *) kUTTypeImage,
                              nil];
imagePicker.allowsEditing = NO;

并将其添加到弹出控制器:

self.imagePickerPopOver = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
    [self.imagePickerPopOver presentPopoverFromRect:CGRectMake(aPosViewA.x, cameraButton_y, 100.0, 30.0) inView:self.detailViewController.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

这些是 UIScrollView 上按钮位置的计算,以在正确的位置显示弹出框:

presentPopoverFromRect:CGRectMake(aPosViewA.x, cameraButton_y, 100.0, 30.0)

我不认为问题出在那儿,因为我已经尝试了几种组合。

我也尝试以全屏模式捕获图像,但该应用程序只允许使用横向模式。如果图像是在纵向模式下拍摄的并且模式视图被关闭,则应用程序也会保持纵向模式。如果模式视图被关闭,我找不到阻止 UIImagePickerController 切换到纵向模式或强制应用程序返回横向模式的方法。

更新

我从这里得到了答案,并且更进一步。

我在创建选择器之后和显示弹出框之前转换视图:

switch ([UIApplication sharedApplication].statusBarOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
            self.imagePicker.view.transform = CGAffineTransformMakeRotation(M_PI/2);
            break;
        case UIInterfaceOrientationLandscapeRight:
            self.imagePicker.view.transform = CGAffineTransformMakeRotation(-M_PI/2);
            break;
        default:
            break;
    }

只要我不转动 iPad,它就可以工作。为此,我正在注册方向更改事件:

[[NSNotificationCenter defaultCenter] addObserver:self  selector:@selector(orientationChanged:)  name:UIDeviceOrientationDidChangeNotification  object:nil];

并更改选择器视图:

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

    if (self.imagePicker) {
        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationLandscapeLeft:
                self.imagePicker.view.transform = CGAffineTransformMakeRotation(M_PI/2);
                break;
            case UIInterfaceOrientationLandscapeRight:
                self.imagePicker.view.transform = CGAffineTransformMakeRotation(-M_PI/2);
                break;
            default:
                break;
        }
    }
}

剩下的问题:正如我在开始时所写的,当照片被拍摄时,它被正确地显示为接受或关闭它。现在也进行了改造。不知何故,我需要知道何时拍摄图像并将其转换回来。

而且,这确实是一个令人讨厌的 hack,可能不适用于下一个 iOS 更新。有人知道如何以更清洁的方式实现它吗?

更新 2

这太讨厌了,我找到了一个更干净的解决方案来解决我的问题,但不是关于弹出框控制器中图像选择器的初始问题的答案,Apple 不推荐,但允许这样做。

我现在对 UIImagePickerController 进行了子类化,如下所示:

@implementation QPImagePickerController

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
    return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);
}

- (BOOL)shouldAutorotate {
    return YES;
}

- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskLandscape;
}

@end

我正在全屏使用图像选择器,而不是在弹出窗口中。到目前为止在 iOS7 中测试。

4

4 回答 4

14

UIImagePickerController 有一个名为 cameraViewTransform 的属性。对此应用 CGAffineTransform 将转换预览图像,但不会转换捕获的图像,因此将被正确捕获。我有你描述的同样的问题,我通过创建我的相机控制器并将它放在一个弹出窗口中解决了它(对于 iOS7),如下所示:

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

[imagePickerController setDelegate:self];

imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

CGFloat scaleFactor=1.3f;

switch ([UIApplication sharedApplication].statusBarOrientation) {

        case UIInterfaceOrientationLandscapeLeft:

            imagePickerController.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * 90 / 180.0), scaleFactor, scaleFactor);

            break;

        case UIInterfaceOrientationLandscapeRight:

            imagePickerController.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * -90 / 180.0), scaleFactor, scaleFactor);

            break;

        case UIInterfaceOrientationPortraitUpsideDown:

            imagePickerController.cameraViewTransform = CGAffineTransformMakeRotation(M_PI * 180 / 180.0);

            break;

            default:
                break;
        }

 __popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];

[__popoverController presentPopoverFromRect:presentationRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];

我还在横向时缩放图像,使其比其他情况下更多地填充取景器。在我看来,这一切都相当讨厌,但是一旦 iOS7.1 到来,我希望能够将其删除。

于 2013-09-28T21:40:48.430 回答
3

我找到了另一种解决方案,它可以根据 Journeyman 提供的答案处理在 UIIMagePickerView 呈现时设备旋转的情况。它还处理视图从 UIOrientationLandscapeRight/UIOrientationLandscapeLeft 旋转回 UIOrientationPortrait 的情况。

我创建了 UIImagePickerController 的子类:

#import <UIKit/UIKit.h>

@interface PMImagePickerController : UIImagePickerController

@end

然后注册它以在设备方向发生变化时接收通知:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fixCameraOrientation:) name:UIDeviceOrientationDidChangeNotification object:nil];

选择器 fixCameraOrientation 包含 Journeyman 的代码和一个额外的案例,包裹在检查中以确保 sourceType 是相机:

- (void)fixCameraOrientation:(NSNotification*)notification
{
    if (self.sourceType == UIImagePickerControllerSourceTypeCamera) {
        CGFloat scaleFactor=1.3f;

        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationLandscapeLeft:
                self.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * 90 / 180.0), scaleFactor, scaleFactor);
                break;


            case UIInterfaceOrientationLandscapeRight:
                self.cameraViewTransform = CGAffineTransformScale(CGAffineTransformMakeRotation(M_PI * -90 / 180.0), scaleFactor, scaleFactor);
                break;

            case UIInterfaceOrientationPortraitUpsideDown:
                self.cameraViewTransform = CGAffineTransformMakeRotation(M_PI * 180 / 180.0);
                break;

            case UIInterfaceOrientationPortrait:
                self.cameraViewTransform = CGAffineTransformIdentity;
                break;

            default:
                break;
        }
    }

}

这里的重要情况是设备方向变为纵向的情况。在这种情况下,需要重置叠加层的视图。在图像选择器视图加载后调用 fixCameraOrientation 选择器也很重要,以防设备旋转:

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self fixCameraOrientation:nil];
}
于 2014-02-27T14:28:30.207 回答
2

我的应用程序中也有类似的情况。然而,预览在 iOS7 中正确旋转,而不是在 iOS8 中。此代码假定您有多个方向。

首先是子类化UIImagePickerController

从顶部开始,添加#import <AVFoundation/AVFoundation.h>到您的 .m 文件。

还要添加一个属性来保存初始方向@property (nonatomic) UIInterfaceOrientation startingOrientation;和另一个用于删除剪辑的条件@property (nonatomic) BOOL didAttemptToRemoveCropping;

打算听几个通知。 UIApplicationDidChangeStatusBarOrientationNotification显然是监听设备旋转。AVCaptureSessionDidStartRunningNotification在相机开始捕捉时被调用。

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidStart:) name:AVCaptureSessionDidStartRunningNotification object:nil];

-captureSessionDidStart:添加条件以验证视图实际上在屏幕上并确保应该显示相机if (self.view.window && self.sourceType == UIImagePickerControllerSourceTypeCamera)。如果是这样,请设置起始方向self.startingOrientation = [UIApplication sharedApplication].statusBarOrientation;

添加与上面相同的-statusBarOrientationDidChange:条件,但这次如果为真,我们将更新相机变换。首先,我们根据初始旋转获得偏移旋转。当您输入UIImagePickerController纵向以外的方向时,这是需要的。

CGFloat startingRotation = ({
    CGFloat rotation;

    switch (self.startingOrientation) {
        case UIInterfaceOrientationPortraitUpsideDown:
            rotation = M_PI;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            rotation = -M_PI_2;
            break;
        case UIInterfaceOrientationLandscapeRight:
            rotation = M_PI_2;
            break;
        default:
            rotation = 0.0f;
            break;
    }

    rotation;
});

接下来,我们将使用当前旋转更新相机变换。

self.cameraViewTransform = CGAffineTransformMakeRotation(({
    CGFloat angle;

    switch ([UIApplication sharedApplication].statusBarOrientation) {
        case UIInterfaceOrientationPortraitUpsideDown:
            angle = startingRotation + M_PI;
            break;
        case UIInterfaceOrientationLandscapeLeft:
            angle = startingRotation + M_PI_2;
            break;
        case UIInterfaceOrientationLandscapeRight:
            angle = startingRotation + -M_PI_2;
            break;
        default:
            angle = startingRotation;
            break;
    }

    angle;
}));

最后,我们将尝试从起始方向移除以 90 度方向呈现的黑条。(这可能只是iOS8的问题。)稍微详细一点,如果我进入UIImagePickerController纵向模式然后旋转到横向,预览的顶部和底部会有黑条。对此的解决方案不是缩放,而是删除超级视图的剪辑。我们只需要尝试一次,所以首先检查我们是否调用了这段代码。还要确保我们只在旋转后才调用此代码。如果它在初始方向被调用,它不会立即工作。

if (!self.didAttemptToRemoveCropping && self.startingOrientation != [UIApplication sharedApplication].statusBarOrientation) {
    self.didAttemptToRemoveCropping = YES;

    [self findClippedSubviewInView:self.view];
}

最后在代码中,-findClippedSubviewInView:我们遍历所有子视图以搜索带有.clipsToBounds = YES. 如果那是真的,我们再做一个条件来验证它的祖先超级视图之一是正确的。

for (UIView* subview in view.subviews) {
    if (subview.clipsToBounds) {
        if ([self hasAncestorCameraView:subview]) {
            subview.clipsToBounds = NO;
            break;
        }
    }

    [self findClippedSubviewInView:subview];
}

在中,-hasAncestorCameraView:我们简单地循环超级视图链,如果其中一个类CameraView在名称中,则返回 true。

if (view == self.view) {
    return NO;
}

NSString* className = NSStringFromClass([view class]);

if ([className rangeOfString:@"CameraView"].location != NSNotFound) {
    return YES;

} else {
    return [self hasAncestorCameraView:view.superview];
}

这就是代码的分解,这里是全部。

#import <AVFoundation/AVFoundation.h>
#import "GImagePickerController.h"

@interface GImagePickerController ()
@property (nonatomic) UIInterfaceOrientation startingOrientation;
@property (nonatomic) BOOL didAttemptToRemoveCropping;
@end

@implementation GImagePickerController

- (instancetype)init {
    self = [super init];
    if (self) {

        if ([[[UIDevice currentDevice] systemVersion] intValue] >= 8) {
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(statusBarOrientationDidChange:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureSessionDidStart:) name:AVCaptureSessionDidStartRunningNotification object:nil];
        }

    }
    return self;
}

- (void)dealloc {
    if ([[[UIDevice currentDevice] systemVersion] intValue] >= 8) {
        [[NSNotificationCenter defaultCenter] removeObserver:self];
    }
}


#pragma mark - Capture Session

- (void)captureSessionDidStart:(NSNotification *)notification {
    if (self.view.window && self.sourceType == UIImagePickerControllerSourceTypeCamera) {
        [self updateStartingOrientation];
    }
}


#pragma mark - Orientation

- (void)updateStartingOrientation {
    self.startingOrientation = [UIApplication sharedApplication].statusBarOrientation;
    [self updateCameraTransform];
}

- (void)updateCameraTransform {
    CGFloat startingRotation = ({
        CGFloat rotation;

        switch (self.startingOrientation) {
            case UIInterfaceOrientationPortraitUpsideDown:
                rotation = M_PI;
                break;
            case UIInterfaceOrientationLandscapeLeft:
                rotation = -M_PI_2;
                break;
            case UIInterfaceOrientationLandscapeRight:
                rotation = M_PI_2;
                break;
            default:
                rotation = 0.0f;
                break;
        }

        rotation;
    });

    self.cameraViewTransform = CGAffineTransformMakeRotation(({
        CGFloat angle;

        switch ([UIApplication sharedApplication].statusBarOrientation) {
            case UIInterfaceOrientationPortraitUpsideDown:
                angle = startingRotation + M_PI;
                break;
            case UIInterfaceOrientationLandscapeLeft:
                angle = startingRotation + M_PI_2;
                break;
            case UIInterfaceOrientationLandscapeRight:
                angle = startingRotation + -M_PI_2;
                break;
            default:
                angle = startingRotation;
                break;
        }

        angle;
    }));

    if (!self.didAttemptToRemoveCropping && self.startingOrientation != [UIApplication sharedApplication].statusBarOrientation) {
        self.didAttemptToRemoveCropping = YES;

        [self findClippedSubviewInView:self.view];
    }
}

- (void)statusBarOrientationDidChange:(NSNotification *)notification {
    if (self.view.window && self.sourceType == UIImagePickerControllerSourceTypeCamera) {
        [self updateCameraTransform];
    }
}


#pragma mark - Remove Clip To Bounds

- (BOOL)hasAncestorCameraView:(UIView *)view {
    if (view == self.view) {
        return NO;
    }

    NSString* className = NSStringFromClass([view class]);

    if ([className rangeOfString:@"CameraView"].location != NSNotFound) {
        return YES;

    } else {
        return [self hasAncestorCameraView:view.superview];
    }
}

- (void)findClippedSubviewInView:(UIView *)view {
    for (UIView* subview in view.subviews) {
        if (subview.clipsToBounds) {
            if ([self hasAncestorCameraView:subview]) {
                subview.clipsToBounds = NO;
                break;
            }
        }

        [self findClippedSubviewInView:subview];
    }
}

@end
于 2014-11-27T06:03:14.823 回答
1

带摄像头的 iPad - 不在弹出窗口中显示。相反,呈现在模态视图控制器中,就像在 iPhone 上一样。(至少从 iOS 7 开始)

于 2014-05-04T21:47:10.050 回答