UIImagePickerController相机视图大小问题

时间:2014-02-11 11:15:25

标签: ios iphone objective-c ios7 uiimagepickercontroller

我正在使用UIImagePickerController和自定义叠加层构建应用。什么是比较两个图像(图像之前和图像之后)。拍摄后照片时,我正在使用与之前图像的自定义叠加(请参阅附图)。

iPhone 5 - ios7 enter image description here

iPhone 4 - iOS7(拍摄图像时) enter image description here

iPhone 4 - iOS 7(拍完照片后) enter image description here

查看iPhone 4和iPhone 5相机视图之间的大小差异。

应用程序适用于iPhone 5屏幕尺寸(ios 6和ios7)。但iPhone 4 / 4s的屏幕尺寸,只适用于iOS6。问题是iphone 4 / 4s(仅适用于ios7),摄像头视图全屏显示。 这意味着,你可以注意到 iPhone 5相机视图尺寸~320 * 427(iOS 6和iOS 7) iPhone 4相机视图尺寸~320 * 427(iOS 6) 但 iPhone 4相机视图尺寸~320 * 480(iOS 7)。

拍摄完图像后,它的实际尺寸为320 * 427。由于这个问题,我无法在iPhone 4 iOS7上使用摄像机视图在图像前对齐(因为它的尖叫是320 * 480)。

有没有人面对这个奇怪的问题。我几乎尝试了一切,但没有运气。有什么想法吗???

这是我在照片叠加前用自定义加载相机视图的片段代码。

- (void)loadCameraWithImage
{
    if (!isLoadedOnce)
    {
    isLoadedOnce = YES;
            UIImagePickerController *cameraView = [[UIImagePickerController alloc] init];
           cameraView.sourceType = UIImagePickerControllerSourceTypeCamera;
           cameraView.wantsFullScreenLayout = NO;

        if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
         [self setEdgesForExtendedLayout:UIRectEdgeNone];
        }


    // crop before image

    UIImage *imgTmpCropped = [self imageByCropping:imgBefore toRect:CGRectMake(0, 0, imgBefore.size.width/2, imgBefore.size.height)];
    UIImage *overleyImage = [[UIImage alloc] initWithCGImage: imgTmpCropped.CGImage
                                                   scale: [UIScreen mainScreen].scale
                                             orientation: UIImageOrientationDownMirrored];

    UIImageView *crosshairView;
    UIImageView *beforeView;

    CGFloat screenHieght = [UIScreen mainScreen].bounds.size.height;

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
        //overleyImage = [UIImage imageNamed:@"overlay_ipad.png"];
        crosshairView = [[UIImageView alloc] initWithImage:overleyImage];
        crosshairView.frame = CGRectMake(0, 0, 768, 1024);
        [crosshairView setUserInteractionEnabled:YES];
        crosshairView.backgroundColor = [UIColor clearColor];

        beforeView = [[UIImageView alloc] initWithImage:overleyImage];
        beforeView.frame = CGRectMake(0, 0, 384, 1024);
        beforeView.alpha = 0.5;
        beforeView.contentMode = UIViewContentModeScaleAspectFill;
    }

    else {
        //overleyImage = [UIImage imageNamed:@"overleyImageAfter.png"];
        crosshairView = [[UIImageView alloc] init];
        beforeView = [[UIImageView alloc] initWithImage:overleyImage];

        if(screenHieght>500){
            crosshairView.frame = CGRectMake(0, 60, 320, 480);
            beforeView.frame = CGRectMake(0, 70, 160, 427);
        }
        else{

            if([[[UIDevice currentDevice] systemVersion] floatValue] <7.0){
                crosshairView.frame = CGRectMake(0, 0, 320, 480);
                beforeView.frame = CGRectMake(0, 0, 160, 427);
            }
            else{
                crosshairView.frame = CGRectMake(0, 0, 320, 480);
                beforeView.frame = CGRectMake(0, 0, 160, 480);
            }

        }

        [crosshairView setUserInteractionEnabled:YES];
        crosshairView.backgroundColor = [UIColor clearColor];
        beforeView.alpha = 0.5;
        beforeView.contentMode = UIViewContentModeScaleToFill;
    }


    //[crosshairView addSubview:beforeView];

    //set our custom overlay view
    cameraView.cameraOverlayView = beforeView;
    cameraView.delegate = (id)self;
    cameraView.showsCameraControls = YES;
    cameraView.navigationBarHidden = YES;
    cameraView.toolbarHidden = YES;
    [cameraView setHidesBottomBarWhenPushed:YES];


    [self.view.window.rootViewController presentViewController:cameraView animated:YES completion:nil];

    isLoadedOnce = NO;
}

}

1 个答案:

答案 0 :(得分:9)

TL; DR

相机的预览在每个设备(4:3)中具有相同的宽高比,而屏幕则没有。假设预览将具有特定的宽高比,并且它将被放置在屏幕上的特定位置。在该区域内绘制叠加层。

<强>长

我们遇到了同样的问题。 (我们的用例是拍摄信用卡照片,其覆盖尺寸与卡片相同)。主要问题是相机预览的比例在每个设备中始终相同(4:3),但不同的手机具有不同的屏幕比例(例如iPhone 4s与iPhone 5),因此需要以不同方式安装预览并使覆盖和裁剪非常困难。

我们的解决方案是(代码有些混乱和黑客,对不起):

    // Adjust camera preview to be a little bit more centered instead of adjusted to the top
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    float cameraAspectRatio = 4.0 / 3.0;
    float imageHeight = screenSize.width * cameraAspectRatio;
    float verticalAdjustment;
    if (screenSize.height - imageHeight <= 54.0f) {
        verticalAdjustment = 0;
    } else {
        verticalAdjustment = (screenSize.height - imageHeight) / 2.0f;
        verticalAdjustment /= 2.0f; // A little bit upper than centered
    }
    CGAffineTransform transform = self.cameraController.cameraViewTransform;
    transform.ty += verticalAdjustment;
    self.cameraController.cameraViewTransform = transform;

    CGRect previewFrame = CGRectMake(0, verticalAdjustment, screenSize.width, imageHeight);
    CardPhotoView *overlayView = [[CardPhotoView alloc] initWithFrame:CGRectMake(0, 0, screenSize.width, screenSize.height) widthPercentageOfCamera:self.widthPercentageOfCamera previewFrame:previewFrame];
    self.overlayView = overlayView;

    self.overlayView.delegate = self;
    self.overlayView.useViewport = YES;
    [self.overlayView setCameraReady:NO];

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

    self.cameraController.showsCameraControls = NO;
    self.cameraController.navigationBarHidden = YES;
    self.cameraController.toolbarHidden = YES;
    self.cameraController.cameraOverlayView = self.overlayView;

    [self presentViewController:self.cameraController animated:NO completion:nil];

<强>解释

self.cameraControllerUIImagePickerControllerCardPhotoView是绘制叠加层的视图子类,它使预览帧确切地知道预览的位置(某些设备顶部和底部将有黑条,有些设备不会)。它还需要一个宽度百分比来为裁剪窗口添加一点填充。 此外,我们隐藏了所有默认的相机控件,我们已经实现了一些按钮,可以为我们完成工作。这些按钮添加在CardPhotoView类中,并且会在考虑预览帧的位置时绘制它们。