点击获取手机相册图片

2016-09-02  本文已影响112人  Dove_Q
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var clousureL: ((image: UIImage) -> Void)!
//    var imageView: UIImageView!
    var btn: UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        btn = UIButton(type: .Custom)
        btn.frame = CGRect(x: 100, y: 100, width: 150, height: 150)
        btn.setImage(UIImage(named: "image.png"), forState: .Normal)
        //给button添加点击事件
        btn.addTarget(self, action: #selector(didButton(_:)), forControlEvents: .TouchUpInside)
        self.view.addSubview(btn)
        //给button添加单击手势
        let tap = UITapGestureRecognizer(target: self, action: #selector(tapClick(_:)))
        btn.addGestureRecognizer(tap)
        
    }

    func didButton(sender: UIButton){
        
        
        didClick()
        clousureL = {
            (image) -> Void in
            self.btn.setImage(image, forState: .Normal)
            
        }
        
    }
    
    func tapClick(sender:UITapGestureRecognizer){
        print("didclick")
        didClick()
        clousureL = {
            self.btn.setImage($0, forState: .Normal)
        }
    }
    
    
    func didClick(){
        print("didclick")
        let actionSheet = UIAlertController(title: "上传头像", message: nil, preferredStyle: .ActionSheet)
        let cancelBtn = UIAlertAction(title: "取消", style: .Cancel, handler: nil)
        let takePhotos = UIAlertAction(title:"拍照",style: .Destructive,handler: {
            (action: UIAlertAction) -> Void in
            if UIImagePickerController.isSourceTypeAvailable(.Camera){
                let picker = UIImagePickerController()
                picker.delegate = self
                picker.sourceType = .Camera
                picker.allowsEditing = true
                self.presentViewController(picker, animated: true, completion: nil)
            }
            else
            {
                print("模拟器中无法打开相机,请在真机中打开")
            }
        })
        let selectPhotos = UIAlertAction(title:"相册",style: .Default,handler: {
            (action: UIAlertAction) -> Void in
            let picker = UIImagePickerController()
            picker.sourceType = .PhotoLibrary
            picker.delegate = self
            picker.allowsEditing = true
            self.presentViewController(picker, animated: true, completion: nil)
        })
        actionSheet.addAction(cancelBtn)
        actionSheet.addAction(takePhotos)
        actionSheet.addAction(selectPhotos)
        self.presentViewController(actionSheet, animated: true, completion: nil)
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let image = info[UIImagePickerControllerOriginalImage] as? UIImage
        clousureL(image: image!)
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

}
上一篇 下一篇

猜你喜欢

热点阅读