swift开发笔记26 使用xib自定义相机界面

前端之家收集整理的这篇文章主要介绍了swift开发笔记26 使用xib自定义相机界面前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

首先创建xib以及一个继承uiview的类,然后在主界面中设置相机的cameraOverlayView 为这个UIview。

创建xib,命名为CameraOverlay


创建类CameraOverlayView,继承UIView

拖个按钮到xib中,并设置xib的custom class:


并把按钮的动作处理函数拖到cameroverlayview中:


把xib的背景色设置为clearColor:


在主界面中,打开相机时,把CameraOverlayView设置为相机的遮罩:

@IBAction func GetPhotoAction(sender: AnyObject) {
        imagePicker =  UIImagePickerController()
        //检测相机是否可用
        let isAvailable = UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
        //必须首先设置sourceType 然后再设置其他属性 否则会出异常,提示sourceType 必须是 UIImagePickerControllerSourceTypeCamera
        imagePicker.sourceType = .PhotoLibrary
        if isAvailable {
           imagePicker.sourceType = .Camera
            //如果有前置摄像头则调用前置摄像头
            imagePicker.cameraDevice = UIImagePickerControllerCameraDevice.Front
            //是否显示控制栏
            imagePicker.showsCameraControls=false
      
           }
          //自定义相机界面
        let myView = NSBundle.mainBundle().loadNibNamed("CameraOverlay",owner: nil,options: nil).first as? CameraOverlayView
        if myView != nil{
            myView?.imagePicker=imagePicker
            myView?.frame = (imagePicker.cameraOverlayView?.frame)!
            // imagePicker.cameraOverlayView=myView
        }
       
        //代理
        imagePicker.delegate = self 
        //打开相机
        presentViewController(imagePicker,animated: true,completion: {
             self.imagePicker.cameraOverlayView = myView
        })
        //是否可编辑
        imagePicker.allowsEditing=false
       
    }
原文链接:https://www.f2er.com/swift/324208.html

猜你在找的Swift相关文章