【问题标题】:Custom Camera View - Cannot change the frame size of the cameraLayout UIImagePicker, Swift自定义相机视图 - 无法更改 cameraLayout UIImagePicker,Swift 的帧大小
【发布时间】:2016-02-29 17:40:23
【问题描述】:

基本上,我正在尝试制作自定义相机视图。我被困在 UIImagePicker 内更改图像视图的大小。图像视图是指显示相机视图的窗口。我希望从图片中会更清楚。就是那个红色方块里面的视图(我手动画了那个红色方块给你看,不要以为是代码)。

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

@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文件,所以请在下面找到它:

按钮显示在它们应该在的位置,但是我无法使用该 imageView。我在这里创建了一个图像视图,以便我可以获取它的 frame.size 并设置为选择器的 frame.size(您可以在我的代码清单中看到这一点)。当然,对该图像视图的引用位于 CustomCameraView 类中,因此您可能也注意到了,我是从该类中获得的。

老实说,我已经尝试了一整天,但我无法在网上找到解决方案。我也读过 AVFoundation 但似乎没有一个明确的解释来说明如何在 AVFoundation 文档中设置该视图的帧大小,就像在 UIImagePicker 中一样,所以我决定问你们,因为我在项目中迷路了,想不出另一个现在的方式。

如果您能帮助我,我将不胜感激。如果您需要更多信息,请告诉我。

谢谢!

【问题讨论】:

  • 您应该设置 imagePicker 的 cameraViewTransform 以使其处于您想要的位置/比例。相机具有适用于不同模式(照片/视频)的原生纵横比,因此不会被设置为任意值。
  • 感谢您的评论。你能给我一个cameraViewTransform用法的小例子吗?我将不胜感激,如果您这样做,请作为答案,以便更容易看到。

标签: ios swift uiimageview uiimagepickercontroller ios-camera


【解决方案1】:

不确定你想如何改变 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),但您必须裁剪生成的图像以匹配显示给用户的内容。

【讨论】:

  • 感谢您的宝贵时间。这个答案真的帮助我理解了这个过程。所以我假设,我可能同时使用了 CGAffineTransformMakeTranslation 和 CGAffineTransformMakeScale。实际上,我确实尝试过缩放,并且它按照我的意愿缩小了尺寸。只是一个简单的问题,如果你让我。当我缩小时,实际上用户看到的更少,但它拍摄的照片尺寸与以前一样,是吗?
  • 是的。您只是缩放预览而不是实际图像。
猜你喜欢
  • 1970-01-01
  • 2015-12-30
  • 2023-03-15
  • 1970-01-01
  • 2016-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多