swift中UIImagePickerController的使用(相机)

前端之家收集整理的这篇文章主要介绍了swift中UIImagePickerController的使用(相机)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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)
}
原文链接:https://www.f2er.com/swift/322786.html

猜你在找的Swift相关文章