Upload Image Button
@IBAction func uploadImageAction(_ sender: UIButton) {
let picker = UIImagePickerController()
picker.sourceType = .photoLibrary
picker.delegate = self
picker.allowsEditing = true
picker.modalPresentationStyle = .fullScreen
present(picker, animated: true)
}
Delegate Functions
extension SignUpViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]){
picker.dismiss(animated: true, completion: nil)
guard let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage else {
return
}
guard let imageData = image.pngData() else {
return
}
storage.child("images/file.png").putData(imageData, metadata: nil) { _, error in
guard error == nil else {
print("Failed to upload")
return
}
self.storage.child("images/file.png").downloadURL { url, error in
guard let url = url, error == nil else{
return
}
let urlString = url.absoluteString
print("Download URL :- \(urlString)")
}
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
// Solve the problem that the cancel button is not sensitive when selecting photos after iOS 11
if (UIDevice.current.systemVersion as NSString).floatValue < 11.0 {
return
}
if (viewController.isKind(of: NSClassFromString("UIImagePickerController")!)){
for (index,obj) in viewController.view.subviews.enumerated() {
if obj.frame.size.width < 42 {
viewController.view.sendSubviewToBack(obj)
}
}
}
}
}
}