Get image from UIImagePickerController in swift (ios)

Set info.plist :- <key>NSPhoto</key>
<string> give parmition to access Photo library</string>
Delegate :- UINavigationControllerDelegate, UIImagePickerControllerDelegate


@IBOutlet var myImageView: UIImageView!
let image = UIImagePickerController()
Override func viewDidLoad()
{
 super.viewDidLoad()
 openPhotoLibrary()
}
// Function for open photo library for your simulator
func openPhotoLibrary()
{
  image.delegate = self
 image.navigationController?.navigationBar.isTranslucent = true
        image.sourceType = UIImagePickerControllerSourceType.photoLibrary
        image.allowsEditing = false
        image.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!
        
        // Image picker controller cancel button
        let myview = UIView()
        myview.frame = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: 44)
        myview.backgroundColor = UIColor(red: 204.0/255.0, green: 163.0/255.0, blue: 171.0/255.0, alpha: 1)
        let btn = UIButton()
        btn.frame = CGRect(x: 200, y: 15, width: UIScreen.main.bounds.size.width - 50, height: 20)
        btn.titleLabel?.textColor = UIColor.white
        btn.titleLabel?.text = "Cancel"
        btn.tintColor = UIColor.white
        btn.setImage(#imageLiteral(resourceName: "Cancel"), for: .normal)
        btn.addTarget(self, action: #selector(CancelProcess), for: .touchUpInside)
        myview.addSubview(btn)
        
        image.navigationBar.addSubview(myview)
        
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
        {
            myImageView.image = image

        }
}
// Button tuchUpInside event
@IBAction func SelectImage(_ sender: UIButton)
    {
      present(image, animated: true, completion: {
        })
    }

// Cancel process
    @objc func CancelProcess()
    {
        dismiss(animated: true, completion: nil)
        popoverPresentationController?.delegate?.popoverPresentationControllerDidDismissPopover?(popoverPresentationController!)

    }

Comments

Popular posts from this blog

Windows Keys

how to send invite and give a access of selected application on App Store

how to display popover in TableView cell in swift