代码之家  ›  专栏  ›  技术社区  ›  kubi

uiImagePickerController不填充屏幕

  •  56
  • kubi  · 技术社区  · 14 年前

    screenshot http://i39.tinypic.com/oiermb.png

    我正在向uiImagePickerController添加自定义覆盖,视图底部有一个持久的黑色条。这是我用来实例化控制器的代码。

    - (UIImagePickerController *)imagePicker {
        if (_imagePicker) {
            return _imagePicker;
        }
    
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
    
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    
            _imagePicker.showsCameraControls = NO;
    
            _imagePicker.wantsFullScreenLayout = YES;
            _imagePicker.navigationBarHidden = YES;
            _imagePicker.toolbarHidden = YES;
    
        } else {
            _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
    
        return _imagePicker;
    }
    

    当我不隐藏相机控件时,返回的控制器以模态显示,并且工作正常(即全屏显示)。


    多亏了Ole的建议,我可以使用此代码:

    // Resize the camera preview
            _imagePicker.cameraViewTransform = CGAffineTransformMakeScale(1.0, 1.03);
    

    身高增加3%的效果很好。当我在屏幕底部添加自定义工具栏时,窗口上不再有可见的黑色条。

    11 回复  |  直到 7 年前
        1
  •  37
  •   Ole Begemann    14 年前

    相机的纵横比为4:3,屏幕的纵横比为3:2。所以,除非你愿意裁剪到3:2,否则相机图片根本无法填充屏幕。为此,请应用适当的比例变换。

        2
  •  51
  •   Steve    11 年前

    按固定值缩放不是个好主意…我敢肯定,在这里使用公认答案的人可能会在iPhone5推出时发现答案。

    下面是一个代码片段,根据屏幕分辨率动态缩放,以消除字母装箱。

    // Device's screen size (ignoring rotation intentionally):
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    
    // iOS is going to calculate a size which constrains the 4:3 aspect ratio
    // to the screen size. We're basically mimicking that here to determine
    // what size the system will likely display the image at on screen.
    // NOTE: screenSize.width may seem odd in this calculation - but, remember,
    // the devices only take 4:3 images when they are oriented *sideways*.
    float cameraAspectRatio = 4.0 / 3.0;
    float imageWidth = floorf(screenSize.width * cameraAspectRatio);
    float scale = ceilf((screenSize.height / imageWidth) * 10.0) / 10.0;
    
    self.ipc.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
    
        3
  •  20
  •   strikerdude10    11 年前

    嘿,我看到一些人在计算完iPhone5的比例后,仍然在底部得到黑条。我有一段时间遇到了这个问题,但后来我发现你必须转换视图,使其位于屏幕中间,然后应用比例。这是我做这两件事的代码,对我有用!

        CGSize screenBounds = [UIScreen mainScreen].bounds.size;
    
        CGFloat cameraAspectRatio = 4.0f/3.0f;
    
        CGFloat camViewHeight = screenBounds.width * cameraAspectRatio;
        CGFloat scale = screenBounds.height / camViewHeight;
    
        m_imagePickerController.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenBounds.height - camViewHeight) / 2.0);
        m_imagePickerController.cameraViewTransform = CGAffineTransformScale(m_imagePickerController.cameraViewTransform, scale, scale);
    
        4
  •  10
  •   emenegro    14 年前

    用这个转换它:

    #define CAMERA_TRANSFORM                    1.12412
    
    _picker.wantsFullScreenLayout = YES;
    _picker.cameraViewTransform = CGAffineTransformScale(_picker.cameraViewTransform, CAMERA_TRANSFORM, CAMERA_TRANSFORM);
    
        5
  •  10
  •   Enrico Cupellini    10 年前

    从我在iOS 7、iPhone 5S上的经验来看,要将图片的中心显示为全屏预览,您必须连接转换,而不是按顺序进行转换:

    pickerController.cameraOverlayView = self.view;
    
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;   // 320 x 568
    
    float scale = screenSize.height / screenSize.width*3/4;  // screen height divided by the pickerController height ... or:  568 / ( 320*4/3 )
    
    CGAffineTransform translate=CGAffineTransformMakeTranslation(0,(screenSize.height - screenSize.width*4/3)*0.5);
    CGAffineTransform fullScreen=CGAffineTransformMakeScale(scale, scale);
    pickerController.cameraViewTransform =CGAffineTransformConcat(fullScreen, translate);
    
        6
  •  4
  •   M.Othman    9 年前

    答案就在这里。

    let screenSize:CGSize = UIScreen.mainScreen().bounds.size
    
    let ratio:CGFloat = 4.0 / 3.0
    let cameraHeight:CGFloat = screenSize.width * ratio
    let scale:CGFloat = screenSize.height / cameraHeight
    
    let imag:UIImagePickerController = UIImagePickerController()
    imag.cameraViewTransform = CGAffineTransformMakeTranslation(0, (screenSize.height - cameraHeight) / 2.0)
    imag.cameraViewTransform = CGAffineTransformScale(imag.cameraViewTransform, scale, scale)
    
        7
  •  3
  •   Ali Gangji    11 年前

    我用这个方法来计算比例。

    // get the screen size
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;
    // establish the height to width ratio of the camera
    float heightRatio = 4.0f / 3.0f;
    // calculate the height of the camera based on the screen width
    float cameraHeight = screenSize.width * heightRatio;
    // calculate the ratio that the camera height needs to be scaled by
    float scale = screenSize.height / cameraHeight;
    imagePicker.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
    
        8
  •  3
  •   Furkan Fidan    8 年前

    这在iOS 10上对我很有用:

    let screenSize = UIScreen.main.bounds.size
    let cameraAspectRatio = CGFloat(4.0 / 3.0)
    let cameraImageHeight = screenSize.width * cameraAspectRatio
    let scale = screenSize.height / cameraImageHeight
    imagePickerController.cameraViewTransform = CGAffineTransform(translationX: 0, y: (screenSize.height - cameraImageHeight)/2)
    imagePickerController.cameraViewTransform = imagePickerController.cameraViewTransform.scaledBy(x: scale, y: scale)
    
        9
  •  2
  •   arunit21    10 年前

    我的问题解决了:)

    //Camera is 426 * 320. Screen height is 568.  Multiply by 1.333 in 5 inch (iPhone5) 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;
    
        10
  •  0
  •   objectively C    7 年前

    尝试使用此代码:

    picker.wantsFullScreenLayout = YES;
    CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 71.0);
    CGAffineTransform scale = CGAffineTransformScale(translate, 1.333333, 1.47);
    picker.view.transform = scale;
    
        11
  •  0
  •   maddie    7 年前
    PickerViewController.m    
    #define CAMERA_TRANSFORM  1.8
    
    - (void)viewDidAppear:(BOOL)animated
    {
     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
           // type = UIImagePickerControllerSourceTypeCamera;
    
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.allowsEditing = NO;
            picker.delegate   = self;
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            picker.showsCameraControls=NO;
            picker.extendedLayoutIncludesOpaqueBars = YES;
            picker.cameraViewTransform = CGAffineTransformScale(picker.cameraViewTransform, CAMERA_TRANSFORM , CAMERA_TRANSFORM);
    
            [self presentViewController:picker animated:YES completion:nil];
        }else{
    
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Message"
                                                            message:@"Device have no camera"
                                                           delegate:self
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alert show];
        }
    }