CameraRoll 方形显示和圆形遮罩 - UIImagePickerController
CameraRoll Square Display and Circular Mask - UIImagePickerController
我正在使用 UIImagePickerController 在相机胶卷上进行试验。我已经实现了使用自定义按钮呈现相机视图(为了实现这一点,我使用了另一个 ViewController 和一个 xib (CustomOverlayVC.swift))。所以我有:
MainViewController.swift(+故事板)
CustomOverlayViewController.swift
CustomOverlayView.swift (UIView)
full code below
在 CustomOverlayVC 上以编程方式创建 NavBar 之后,这是目前的样子(也是我想要在右边实现的)
所以我的问题是:
1) 如何像第二张图片一样缩小图像选择器的视图(显示效果与拍摄正方形照片一样好?)
我尝试了 picker.view.frame.height -= 40
但显然它只能得到 属性.
2) 如何在显示圆形遮罩的图像上添加遮罩? (针对 objective-c here 进行了解释,但我不知道任何 obj-c,无法将其成功转换为 Swift)
3) 我试图用 picker.view.backgroundColor = UIColor.redColor()
给它一个自定义背景颜色,但是,它根本不起作用。要清除,在第二个示例中它具有灰色背景颜色。
4) 此外,我创建的导航栏看起来更宽,即使我使用过,(我知道这是一个不同的问题,但如果能解决它,我们将不胜感激)
UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
所以总结一下,我想缩小它的视图高度直到它变成正方形(以及拍摄正方形的照片)(第二张图片),同时,还有一个圆形遮罩,圆外变灰(最后一张图)
我创建了一个 sample project,下面还有完整的代码..
ViewController.swift
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var picker = UIImagePickerController()
@IBAction func shootCamera(sender: AnyObject) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker = UIImagePickerController() //make a clean controller
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraDevice = .Front
picker.cameraCaptureMode = .Photo
picker.showsCameraControls = false
picker.delegate = self
let customViewController = CameraCustomOverlayViewController(nibName:"CameraCustomOverlayViewController",bundle: nil)
let customView:CameraCustomOverlayView = customViewController.view as! CameraCustomOverlayView
customView.frame = self.picker.view.frame
picker.modalPresentationStyle = .FullScreen
presentViewController(picker,animated: true,completion: {
self.picker.cameraOverlayView = customView
})
} else { //no camera found -- alert the user.
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .Alert)
let okAction = UIAlertAction(
title: "OK",
style:.Default,
handler: nil)
alertVC.addAction(okAction)
presentViewController(
alertVC,
animated: true,
completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil)
}
//What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
CustomOverlayVC.swift(对于导航栏)
class CameraCustomOverlayViewController: UIViewController, UINavigationBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
navigationBar.backgroundColor = UIColor.blackColor()
navigationBar.delegate = self;
let navigationItem = UINavigationItem()
navigationItem.title = "CAMERAROLL"
let leftButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(CameraCustomOverlayViewController.btn_clicked(_:)))
let rightButton = UIBarButtonItem(title: "Save", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
navigationBar.items = [navigationItem]
self.view.addSubview(navigationBar)
}
}
编辑:
为了理解高度,我尝试添加彩色边框,它覆盖了整个视图而不是 ImagePicker 对我的视图...我想不出任何解决方法..
picker.view.layer.borderWidth = 1
picker.view.layer.borderColor = UIColor.redColor().CGColor
我从 UIImagePickerController 转移到 AVFoundation 方法来完成更多自定义相机胶卷
我正在使用 UIImagePickerController 在相机胶卷上进行试验。我已经实现了使用自定义按钮呈现相机视图(为了实现这一点,我使用了另一个 ViewController 和一个 xib (CustomOverlayVC.swift))。所以我有:
MainViewController.swift(+故事板)
CustomOverlayViewController.swift
CustomOverlayView.swift (UIView)
full code below
在 CustomOverlayVC 上以编程方式创建 NavBar 之后,这是目前的样子(也是我想要在右边实现的)
所以我的问题是:
1) 如何像第二张图片一样缩小图像选择器的视图(显示效果与拍摄正方形照片一样好?)
我尝试了 picker.view.frame.height -= 40
但显然它只能得到 属性.
2) 如何在显示圆形遮罩的图像上添加遮罩? (针对 objective-c here 进行了解释,但我不知道任何 obj-c,无法将其成功转换为 Swift)
3) 我试图用 picker.view.backgroundColor = UIColor.redColor()
给它一个自定义背景颜色,但是,它根本不起作用。要清除,在第二个示例中它具有灰色背景颜色。
4) 此外,我创建的导航栏看起来更宽,即使我使用过,(我知道这是一个不同的问题,但如果能解决它,我们将不胜感激)
UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
所以总结一下,我想缩小它的视图高度直到它变成正方形(以及拍摄正方形的照片)(第二张图片),同时,还有一个圆形遮罩,圆外变灰(最后一张图)
我创建了一个 sample project,下面还有完整的代码..
ViewController.swift
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var picker = UIImagePickerController()
@IBAction func shootCamera(sender: AnyObject) {
if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
picker = UIImagePickerController() //make a clean controller
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.Camera
picker.cameraDevice = .Front
picker.cameraCaptureMode = .Photo
picker.showsCameraControls = false
picker.delegate = self
let customViewController = CameraCustomOverlayViewController(nibName:"CameraCustomOverlayViewController",bundle: nil)
let customView:CameraCustomOverlayView = customViewController.view as! CameraCustomOverlayView
customView.frame = self.picker.view.frame
picker.modalPresentationStyle = .FullScreen
presentViewController(picker,animated: true,completion: {
self.picker.cameraOverlayView = customView
})
} else { //no camera found -- alert the user.
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .Alert)
let okAction = UIAlertAction(
title: "OK",
style:.Default,
handler: nil)
alertVC.addAction(okAction)
presentViewController(
alertVC,
animated: true,
completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
UIImageWriteToSavedPhotosAlbum(chosenImage, self,nil, nil)
}
//What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
CustomOverlayVC.swift(对于导航栏)
class CameraCustomOverlayViewController: UIViewController, UINavigationBarDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let navigationBar = UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))
navigationBar.backgroundColor = UIColor.blackColor()
navigationBar.delegate = self;
let navigationItem = UINavigationItem()
navigationItem.title = "CAMERAROLL"
let leftButton = UIBarButtonItem(title: "Back", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(CameraCustomOverlayViewController.btn_clicked(_:)))
let rightButton = UIBarButtonItem(title: "Save", style: UIBarButtonItemStyle.Plain, target: self, action: nil)
navigationItem.leftBarButtonItem = leftButton
navigationItem.rightBarButtonItem = rightButton
navigationBar.items = [navigationItem]
self.view.addSubview(navigationBar)
}
}
编辑:
为了理解高度,我尝试添加彩色边框,它覆盖了整个视图而不是 ImagePicker 对我的视图...我想不出任何解决方法..
picker.view.layer.borderWidth = 1
picker.view.layer.borderColor = UIColor.redColor().CGColor
我从 UIImagePickerController 转移到 AVFoundation 方法来完成更多自定义相机胶卷