美文网首页
点击获取手机相册图片

点击获取手机相册图片

作者: Dove_Q | 来源:发表于2016-09-02 10:17 被阅读112次
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)
    }

}

相关文章

  • 点击获取手机相册图片

  • 点击头像更换图片

    功能如下: 1.点击头像,提示选择更换头像方式①相册 ②照相. 2.点击相册,实现通过读取系统相册,获取图片进行替...

  • 项目总结 - 8.2

    如何获取手机系统相册中的图片 单张 多张 示例代码

  • iOS保存(获取)图片到相册

    iOS保存图片到相册 二 : 获取系统的相册图片(一张或者多张) 1.获取一张相册里的图片(图片 == 1) 获取...

  • UIImagePickerController

    效果 有一个头像按钮,点击进入相册,从相册中获取图片,更改按钮的图片为选取的图片 创建一个自定义UIButton按...

  • 热门手机壁纸

    点击图片保存手机(苹果(Android)壁纸-长按图片-保存图片。保存后,返回相册并将其设置为手机壁纸!因为都是高...

  • iPhone壁纸:豪车壁纸

    点击图片保存手机(苹果(Android)壁纸-长按图片-保存图片。保存后,返回相册并将其设置为手机壁纸!因为都是高...

  • 苹果Android壁纸

    点击图片保存手机(苹果(Android)壁纸-长按图片-保存图片。保存后,返回相册并将其设置为手机壁纸!因为都是高...

  • 高清手机壁纸

    点击图片保存手机(苹果(Android)壁纸-长按图片-保存图片。保存后,返回相册并将其设置为手机壁纸!因为都是高...

  • 手机壁纸:6.20精美壁纸

    iPhone手机壁纸(苹果(安卓)壁纸)保存方法:点击图片-长按图片-保存图片,保存完毕返回相册即可设置为手机壁纸...

网友评论

      本文标题:点击获取手机相册图片

      本文链接:https://www.haomeiwen.com/subject/kdvhettx.html