如何让UIImagePicker相机的自定义叠加在iOS 7中全屏显示? [英] How to get the custom overlay for UIImagePicker camera to be full screen in iOS 7?

查看:149
本文介绍了如何让UIImagePicker相机的自定义叠加在iOS 7中全屏显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣为整个屏幕的相机控件使用 UIImagePickerController 的自定义叠加层,非常类似于切换到视频时默认的相机应用程序所执行的操作功能。

I am interested in using a custom overlay for the UIImagePickerController for camera controls that take the entire screen, very similar to what the default Camera app does when switched to the video function.

关于这个主题我在SO上引用了几个问题,特别是一个,以及 Apple示例关于如何为UIImagePickerController使用自定义叠加层,但是没有一个能够使用iOS 7生成成功的全屏结果。

I have referenced several questions here on SO on this topic, particularly this one, as well as the Apple example on how to use a custom overlay for the UIImagePickerController, but none of them have been able to produce a successful, full-screen result with iOS 7.

到目前为止,这是相机用户界面与自定义叠加层(没有任何按钮)的样子:

So far, this is what the camera UI looks like with the custom overlay (without any buttons):

请注意屏幕底部有一个黑条。我注意到,如果我将 cameraAspectRatio 更改为1,我可以删除黑条,但这会使相机变焦,因为它有一个非常放大视野。有没有办法让全屏没有太多扭曲变焦(我明白一点点是必要的),还删除黑条?

Note that there is a black bar in the bottom of the screen. I noticed that I could get the black bar removed if I change the cameraAspectRatio to 1, but this distorts the camera zoom as it then has a very zoomed in view. Is there a way to have full screen without either distorting the zoom too much (I understand that a small bit is necessary), and also remove the black bar?

以下是配置自定义叠加层的代码:

Here is my code that configures the custom overlay:

{
        self.imagePickerController.showsCameraControls = NO;

        [[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
        self.overlayView.frame = self.imagePickerController.cameraOverlayView.frame;
        self.imagePickerController.cameraOverlayView = self.overlayView;
        self.overlayView = nil;

        // Device's screen size (ignoring rotation intentionally):
        CGSize screenSize = [[UIScreen mainScreen] bounds].size;


        float cameraAspectRatio = 4.0 / 3.0; //! Note: 4.0 and 4.0 works
        float imageWidth = floorf(screenSize.width * cameraAspectRatio);
        float scale = ceilf((screenSize.height / imageWidth) * 10.0) / 10.0;

        self.imagePickerController.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);

    }

更新:

我注意到的另一个问题是,相机预览中显示的图片总是较小,其细节少于图片时保存的图片。方法 [self.imagePickerController takePicture] 。为了说明:

An additional issue that I noticed is, the picture that is shown in camera preview is always smaller and has less details than the picture that is saved when the method [self.imagePickerController takePicture]. To illustrate:

这是键盘在相机预览中的样子,下面是黑色条(抱歉它有点不稳定):

This is what the keyboard looks like in the camera preview with the black bar below (sorry its a bit shaky):

但是,请注意,预览面板中的实际捕获图像具有更多详细信息,如此处所示,尤其是顶部,左侧和右侧。

However, note that the actual captured image in the preview panel has a lot more details as shown here, especially towards the top, as well as both left and right.

我的问题是,如何设置我的相机预览,以便用户在预览中看到的是它将捕获的图像,然后可以显示给他们?谢谢!

My question is, how would be able to set my camera preview so that what the user sees in preview is exactly the image that it will capture and could be shown to them afterwards? Thanks!

更新2

以下是<$ c中的完整代码$ c> viewDidLoad 设置相机控件。

- (void)viewDidLoad
{
    [super viewDidLoad];

    //Appearance configuration
    self.navigationItem.hidesBackButton = YES;

    //UIImagePickerController initialization and setup
    self.imagePickerController = [[UIImagePickerController alloc] init];
    self.imagePickerController.delegate = self;
    self.imagePickerController.allowsEditing = NO;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; //if there is a camera avaliable
    } else {
        self.imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//otherwise go to the folder
    }
    self.imagePickerController.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
    if (self.imagePickerController.sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        self.imagePickerController.showsCameraControls = NO;

        [[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
        self.overlayView.frame = self.imagePickerController.cameraOverlayView.frame;
        self.imagePickerController.cameraOverlayView = self.overlayView;
        self.overlayView = nil;

        // Device's screen size (ignoring rotation intentionally):
        CGSize screenSize = [[UIScreen mainScreen] bounds].size;

        int heightOffset = 0;

        if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
        {
            heightOffset = 120; //whole screen :)
        }

        float cameraAspectRatio = 4.0 / 3.0; //! Note: 4.0 and 4.0 works
        float imageWidth = floorf(screenSize.width * cameraAspectRatio);
        float scale = ceilf(((screenSize.height + heightOffset) / imageWidth) * 10.0) / 10.0;

        self.imagePickerController.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);

    }
}

以及 viewWillAppear ,我调用图像选择器视图:

And in viewWillAppear, I call the image picker view:

BOOL modalPresent = (BOOL)(self.presentedViewController);
    //Present the Camera UIImagePicker if no image is taken
    if (!appDelegate.imageStorageDictionary[@"picture1"]){
        if (modalPresent == NO){ //checks if the UIImagePickerController is modally active
            [self presentViewController:self.imagePickerController animated:NO completion:nil];
        }
    }


推荐答案

之后许多尝试,这对我有用,非常感谢其他人的建议。以下事实非常有助于了解并牢记:

After many attempts, this is what worked for me with many thanks to other people's suggestions. The following facts were very helpful to know and keep in mind:

相机的分辨率 426 * 320 。为了将相机预览的高度拉伸至手机屏幕高度568,使用 CGAffineTransformScale 时,需要乘以系数1.3333。

The camera's points resolution is 426 * 320. In order for the camera preview's height to be stretched to the phone's screen height of 568, it needs to be multiplied by a factor of 1.3333 when using CGAffineTransformScale.

请注意,以下内容是根据iPhone 5的分辨率屏幕分辨率使用各种数字进行硬编码的。它们可以通过使用诸如screen.height,screen.width和其他变量之类的对象来改进,以使其适用于iPhone 4 / 4s尺寸。

Note that the below are hard coded with various numbers based on the iPhone 5's screen resolution in points. They could be improved by using such objects such as screen.height, screen.width and other variables to make it applicable to iPhone 4/4s dimensions as well.

    self.imagePickerController.showsCameraControls = NO;

    [[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self options:nil];
    self.overlayView.frame = self.imagePickerController.cameraOverlayView.frame;
    self.imagePickerController.cameraOverlayView = self.overlayView;
    self.overlayView = nil;

    //For iphone 5+
    //Camera is 426 * 320. Screen height is 568.  Multiply by 1.333 in 5 inch to fill vertical
    CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0); //This slots the preview exactly in the middle of the screen by moving it down 71 points
    self.imagePickerController.cameraViewTransform = translate;

    CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.333333);
    self.imagePickerController.cameraViewTransform = scale;

这篇关于如何让UIImagePicker相机的自定义叠加在iOS 7中全屏显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆