2016-02-29 52 views
1

我正在尝试自定义摄像头视图,基本上。我卡住更改UIImagePicker内部图像视图的大小。通过图像视图,我的意思是显示相机视图的窗口。我希望从图中可以看得更清楚。它是那个红色方块内的视图(我手动绘制了这个红色方块向你展示,不认为它来自代码)。自定义摄像头视图 - 无法更改cameraLayout UIImagePicker的帧大小,Swift

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) 
    } 
} 

我试图从厦门国际银行文件,完成整个自定义视图的事情,它关系到CustomCameraView类。我为CustomCameraView设置了两个委托方法,可以使用这些方法,但这些对于此问题不是必需的,所以我不会发布代码,但仍然认为您可能需要知道。什么是相关的问题是,厦门国际银行文件,所以请在下面找到它:

CustomCameraViewController.xib file

的按钮都出现在它们应该是,但是我不能让使用该ImageView的组成。我在这里创建了一个图像视图,以便我可以将其frame.size并设置为选择器的frame.size(您可以在我的代码清单中看到这一点)。而且对于这个图像视图的引用是在CustomCameraView类中的,所以我正在从这个类中伸出,你也许会注意到。

我一直在尝试一整天,但我无法找到一个解决方案,无论是在网上或我自己。我也读过AVFoundation,但似乎没有如何在UFoundation文档中设置该视图的帧大小的明确解释,因为我在UIImagePicker中决定问你们,因为我在项目中迷失了方向,无法想象另一个现在的方式。

如果你能帮助我,我将非常感激。让我知道你是否需要进一步的信息。

谢谢!

+0

您应该设置imagePicker的cameraViewTransform以获得您想要的位置/比例。相机具有适用于不同模式(照片/视频)的原生宽高比,因此可抵抗设置为某些任意值。 – beyowulf

+0

感谢您的评论。你能给我一个使用cameraViewTransform的小例子吗?我会很感激,如果你这样做,请做回答,以便更容易看到。 –

回答

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),但你将不得不裁剪产生的图像,以匹配已经显示给用户。

+0

感谢您的时间。这个答案真的帮助我理解这个过程。所以我假设,我可能同时使用CGAffineTransformMakeTranslation和CGAffineTransformMakeScale。我确实尝试过缩放,并缩小了尺寸。只是一个简单的问题,如果你让我。当我缩小尺寸时,实际上用户看到的更少,但它以前所用的相同尺寸拍摄照片,是吗? –

+0

是的。你只是缩放预览而不是实际的图像。 – beyowulf

+0

非常感谢! –