自定义相机视图 - 无法更改 cameraLayout UIImagePicker 的框架大小,Swift
Custom Camera View - Cannot change the frame size of the cameraLayout UIImagePicker, Swift
基本上,我正在尝试制作自定义相机视图。我无法更改 UIImagePicker 内图像视图的大小。通过图像视图,我的意思是 window 显示相机的视图。我希望从图片上看会更清楚。就是那个红色方块里面的图(那个红色方块是我自己手工画出来给大家看的,不要以为是代码出来的)
到目前为止我所做的一切都无法帮助我改变那个红色矩形区域的大小。以下是我目前所拥有的:
@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 class 相关。我为 CustomCameraView 设置了两个委托方法,以便能够使用它们,但这些对于这个问题不是必需的,所以我不会 post 代码,但我仍然认为你可能需要知道。与问题相关的是xib文件,请在下面找到:
按钮显示在它们应该出现的位置,但是我无法使用那个 imageView。我在这里创建了一个图像视图,这样我就可以获取它的 frame.size 并设置为选择器的 frame.size(您可以在我的代码清单中看到它)。当然,对该图像视图的引用在 CustomCameraView class 中,所以我从那个 class 开始,您可能也会注意到。
老实说,我已经尝试了一整天,但我无法在网上找到解决方案。我也读过 AVFoundation 但似乎没有关于如何在 AVFoundation 文档中设置该视图的框架大小的明确解释,就像在 UIImagePicker 中一样,所以我只是决定问你们,因为我在项目中迷失了,不能再想了现在的方式。
如果你能帮助我,我将不胜感激。如果您需要更多信息,请告诉我。
谢谢!
不确定您要如何更改 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) 但你必须裁剪生成的图像以匹配已显示给用户的图像。
基本上,我正在尝试制作自定义相机视图。我无法更改 UIImagePicker 内图像视图的大小。通过图像视图,我的意思是 window 显示相机的视图。我希望从图片上看会更清楚。就是那个红色方块里面的图(那个红色方块是我自己手工画出来给大家看的,不要以为是代码出来的)
到目前为止我所做的一切都无法帮助我改变那个红色矩形区域的大小。以下是我目前所拥有的:
@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 class 相关。我为 CustomCameraView 设置了两个委托方法,以便能够使用它们,但这些对于这个问题不是必需的,所以我不会 post 代码,但我仍然认为你可能需要知道。与问题相关的是xib文件,请在下面找到:
按钮显示在它们应该出现的位置,但是我无法使用那个 imageView。我在这里创建了一个图像视图,这样我就可以获取它的 frame.size 并设置为选择器的 frame.size(您可以在我的代码清单中看到它)。当然,对该图像视图的引用在 CustomCameraView class 中,所以我从那个 class 开始,您可能也会注意到。
老实说,我已经尝试了一整天,但我无法在网上找到解决方案。我也读过 AVFoundation 但似乎没有关于如何在 AVFoundation 文档中设置该视图的框架大小的明确解释,就像在 UIImagePicker 中一样,所以我只是决定问你们,因为我在项目中迷失了,不能再想了现在的方式。
如果你能帮助我,我将不胜感激。如果您需要更多信息,请告诉我。
谢谢!
不确定您要如何更改 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) 但你必须裁剪生成的图像以匹配已显示给用户的图像。