加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift中UIImagePickerController的使用(相机)

发布时间:2020-12-14 06:39:47 所属栏目:百科 来源:网络整理
导读:https://github.com/potato512/SYSwiftLearning 效果图 源代码(关键代码) // 定义属性变量(注意添加代理)var cameraPicker:UIImagePickerController! = nil // MARK: 相机func cameraShow(){ // 是否有相机设备 if UIImagePickerController.isValidCamer

https://github.com/potato512/SYSwiftLearning

效果图


源代码(关键代码)

// 定义属性变量(注意添加代理)
var cameraPicker:UIImagePickerController! = nil

// MARK: 相机
func cameraShow()
{
        // 是否有相机设备
        if UIImagePickerController.isValidCameraEnable
        {
            // 是否支持相机 Camera
            if UIImagePickerController.isValidCamera
            {
                // 是否支持图片库
                if self.cameraPicker == nil
                {
                    self.cameraPicker = UIImagePickerController()
                    self.cameraPicker.sourceType = .Camera
                    self.cameraPicker.delegate = self
                    self.cameraPicker.setImagePickerStyle(UIColor.yellowColor(),titleColor: UIColor.redColor(),buttonTitleColor: UIColor.blackColor())
                }
             
                self.presentViewController(self.cameraPicker,animated: true,completion: nil)
            }
            else
            {
                print("相机打开失败")
            }
        }
        else
        {
            print("没有相机设备")
        }
}
// MARK: - 属性设置
func setImagePickerStyle(bgroundColor:UIColor,titleColor:UIColor,buttonTitleColor:UIColor)
{
        // 改navigationBar背景色
        if let bgroundColorTmp:UIColor = bgroundColor
        {
            self.navigationBar.barTintColor = bgroundColorTmp
        }
        
        // 改navigationBar标题色
        if let titleColorTmp:UIColor = titleColor
        {
            self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: titleColorTmp]
        }
        
        // 改navigationBar的button字体色
        if let buttonTitleColorTmp:UIColor = buttonTitleColor
        {
            self.navigationBar.tintColor = buttonTitleColorTmp
        }
}
// MARK: UIImagePickerControllerDelegate,UINavigationControllerDelegate
func imagePickerController(picker: UIImagePickerController,didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        print("1 图片选择:(info)")
        
        
        // 拍照
        if picker.isEqual(self.cameraPicker)
        {
            let pickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
            
            // 显示图片
            self.imageview.image = pickedImage
        }
        
        // 退出图片选择控制器
        picker.dismissViewControllerAnimated(true,completion: nil)
}
    
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        print("2 放弃图片选择")
        
        // 退出图片选择控制器
        picker.dismissViewControllerAnimated(true,completion: nil)
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读