自定义相机视图 - 无法更改cameraLayout UIImagePicker,Swift的帧大小

时间:2016-02-29 17:40:24

标签: ios swift uiimageview uiimagepickercontroller ios-camera

我试图制作自定义相机视图,基本上。我无法更改UIImagePicker内部图像视图的大小。通过图像视图,我的意思是显示摄像机视图的窗口。我希望从图片中可以更清楚。它是红色方块内部的视图(我手动绘制红色方块向您显示,不要认为它来自代码)。

sample camera view

无论我到目前为止尝试过什么都无法帮助我改变那个红色矩形区域的大小。以下是我到目前为止:

@IBAction func takePhoto(sender: UIButton) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
        imagePicker =  UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = .Camera
        imagePicker.allowsEditing = false
        imagePicker.showsCameraControls = false

        //Create view
        let customViewController = CustomCameraViewController(
            nibName:"CustomCameraViewController",
            bundle: nil
        )
        let customView:CustomCameraView = customViewController.view as! CustomCameraView

        //Assignments
        customView.frame = self.imagePicker.view.frame
        customView.delegate = self
        self.imagePicker.cameraOverlayView?.frame.size = customView.image.frame.size

        presentViewController(imagePicker,
            animated: true,
            completion: {
                self.imagePicker.cameraOverlayView = customView
            }
        )
    }else{
        //Alert
        let alert = UIAlertController(title: "Üzgünüz, kameranızı açamadık!", message: "Telefonunuz kamerayı desteklemiyor veya açmamıza izin verilmemiş. İzin vermek isterseniz ayarlardan izni açabilirsiniz.", preferredStyle: UIAlertControllerStyle.Alert)

        //Creating actions
        let okAction = UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default){
            UIAlertAction in
            NSLog("OK is Pressed. Proceeding...")
        }

        //Adding actions
        alert.addAction(okAction)

        //Display alert
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

我正在尝试从xib文件中完成整个自定义视图事务,它与CustomCameraView类相关。我为CustomCameraView设置了两个委托方法,但是这些方法对于这个问题不是必需的,所以我不会发布代码,但我认为你可能还需要知道。与此问题相关的是xib文件,请在下面找到:

CustomCameraViewController.xib file

按钮显示在它们应该的位置但是我无法使用该imageView。我在这里创建了一个图像视图,以便我可以将其frame.size设置为选择器的frame.size(您可以在我的代码清单中看到这一点)。而对于coruse,对该图像视图的引用是在CustomCameraView类中,所以我也从那个类到达,你也可能会注意到。

我一直在努力工作一整天但我无法在网上找到解决方案。我也读过AVFoundation,但似乎没有一个明确的解释如何在AVFoundation文档中设置该视图的帧大小,如在UIImagePicker中所以我只是决定问你们,因为我在项目中迷失了,不能想到另一个现在的方式。

如果你能帮助我,我将非常感激。如果您需要更多信息,请与我们联系。

谢谢!

1 个答案:

答案 0 :(得分:4)

不确定如何更改cameraView的框架,但您可以说出如下内容,将视图向下移动100个点:

 if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
            imagePicker =  UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = .Camera
            imagePicker.allowsEditing = false
            imagePicker.showsCameraControls = false

            //Create view
            let customViewController = CustomCameraViewController(
                nibName:"CustomCameraViewController",
                bundle: nil
            )
            let customView:CustomCameraView = customViewController.view as! CustomCameraView

            //Assignments
            customView.frame = self.imagePicker.view.frame
            customView.delegate = self
            imagePicker.cameraOverlayView = customView
            //adjust the cameraView's position
            imagePicker.cameraViewTransform = CGAffineTransformMakeTranslation(0.0, 100.0);

            presentViewController(imagePicker,
                animated: true,
                completion: {
                }
            )
        }

如果你想缩放cameraView以占用更多的屏幕,你可以说CGAffineTransformMakeScale(scaleX,scaleY),但是你必须裁剪得到的图像以匹配向用户显示的图像。