打开系统相机和相册


import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var imgBtn: UIButton!
    @IBAction func btnAction(sender: UIButton) {
        
        //弹出选项框
        //a.创建警告框对象
        
        let alert = UIAlertController.init(title: "选择头像", message: nil, preferredStyle: .ActionSheet)
        
        //1.相册
        let action1 = UIAlertAction.init(title: "相册", style: .Default) { (_) -> Void in
            print("打开相册")
            self.openPhotograph()
        }
        
        //2.相机
        let action2 = UIAlertAction.init(title: "相机", style: .Default) { (_) -> Void in
            print("打开相机")
            self.openCamera()
        }
        //3.取消
        let action3 = UIAlertAction.init(title: "取消", style: .Cancel) { (_) -> Void in
        }
        alert.addAction(action1)
        alert.addAction(action2)
        alert.addAction(action3)
        //c.弹出警告框
        self.presentViewController(alert, animated: true) { () -> Void in
            
        }
    }
   


}

//MARK: - 打开相册
extension ViewController: UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    
    //MARK: - 相机
    func openCamera() {
        //1.判断当前设备是否可以获取相机
        let haveCamera = UIImagePickerController.isSourceTypeAvailable(.Camera)
        
        if haveCamera == false {
            print("不支持相机")
            return
        }
        
        //2.创建图片选择器对象
        let imagePicker = UIImagePickerController()
        
        //3.设置资源类型为相机
        imagePicker.sourceType = .Camera
        
        //4.允许图片编辑
        imagePicker.allowsEditing = true
        
        //5.设置代理
        imagePicker.delegate = self
        
        //6.显示图片选择器
        self.presentViewController(imagePicker, animated: true, completion: nil)
    }
    
    //MARK: -  相册
    func openPhotograph() {
        
        //1.创建图片选择器
        let imagePicker = UIImagePickerController.init()
        
        //2.设置
        //Camera -> 图片从相机获得
        //PhotoLibray -> 相册
        //SavedPhotosAlbum -> 图册(包括保存到手机中的图片)
        imagePicker.sourceType = .PhotoLibrary
        
        //4.设置代理
        imagePicker.delegate = self
        
        //3.显示图片选择器
        self.presentViewController(imagePicker, animated: true, completion: nil)
        //5.设置图片可以编辑
        imagePicker.allowsEditing = true
        
    
    }
    
    //选中相册中的一张图片后会自动调用的方法
    //参数2:s所选中的媒体的所有的信息列表

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        //"UIImagePickerControllerOriginalImage" -> 被选中的图片对象
        //1.
        //a.获取选中的图片的原图
        let image1 = info["UIImagePickerControllerOriginalImage"]
        as! UIImage
        //b.获取编辑之后的图片
        let image2 = info["UIImagePickerControllerEditedImage"]
            as! UIImage

        //2.将图片显示在按钮上
        self.imgBtn.setImage(image2, forState: .Normal)
        //3.让图片选择器消失
        picker.dismissViewControllerAnimated(true, completion: nil)
         print(info)
    }
}


推荐阅读更多精彩内容