Add  UIImagePickerControllerDelegate, UINavigationControllerDelegate  at top of the class.

 

Before viewDidLoad()

var picker = UIImagePickerController()

 

In viewDidLoad() or any function that you want to use image picker

picker.delegate = self

 

Add some function for catch event

// What to do when the picker returns with a photo
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    let chosenImage = info[UIImagePickerControllerEditedImage] as! UIImage
    dismissViewControllerAnimated(true, completion: nil)
    addAvatar(chosenImage)
//Use image that user pick from library
}

// What to do if the image picker cancels.
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
    dismissViewControllerAnimated(true, completion: {() in
                // some code for do anything when user cancel to import image
    })
}