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()
{
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
{
myImageView.image = image
}
}
<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!)
}
// Cancel process
@objc func CancelProcess()
{
dismiss(animated: true, completion: nil)
popoverPresentationController?.delegate?.popoverPresentationControllerDidDismissPopover?(popoverPresentationController!)
}
Comments
Post a Comment