CameraRoll Square显示和圆形蒙版 - UIImagePickerController

时间:2016-04-18 00:01:37

标签: ios swift uiviewcontroller uiimagepickercontroller mask

我正在使用UIImagePickerController对Camera roll进行试验。我已经实现了使用自定义按钮呈现相机视图(为实现这一点,我使用了另一个带有xib的ViewController(CustomOverlayVC.swift))。所以我有:

  • MainViewController.swift(+ Storyboard)

  • CustomOverlayViewController.swift

  • CustomOverlayView.swift(UIView)

full code below

在CustomOverlayVC上以编程方式创建NavBar之后,这就是它到目前为止的样子(以及我想在右边实现的内容)

enter image description here enter image description here

所以我的问题是:

1)如何缩小图像拾取器的视图,就像第二张图像一样(显示为以方格格式拍摄图片?)

我尝试了picker.view.frame.height -= 40,但显然它只获得了财产。

2)如何在将显示圆形遮罩的图像上设置遮罩? (这是针对objective-c here解释的,但我不知道任何obj-c并且无法将其成功转换为Swift)

enter image description here

3)我试图用picker.view.backgroundColor = UIColor.redColor()给它一个自定义的背景颜色但是,它根本不起作用。要清除,在第二个示例中,它具有浅灰色背景颜色。

4)此外,我创建的导航栏看起来更宽,即使我使用了(我知道这是一个不同的问题,但解决它会得到高度赞赏)

UINavigationBar(frame: CGRectMake(0, 0, self.view.frame.size.width, 44))

所以要结束,我想缩小视图的高度,直到它变成正方形(以及拍摄正方形照片)(第2张图片),并且,在同时,在圆圈外面有一个圆形的面具和灰色(最后一张图片)

我创建了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(对于NavBar)

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

enter image description here

1 个答案:

答案 0 :(得分:1)

我从UIImagePickerController转移到AVFoundation方法来完成更多自定义相机滚动