-1

Just a heads up I'm new to IOS development and Swift in particular. I have created a "Edit Profile Page" in a view controller that is attached to a regular "profile" view controller. On this edit profile view controller, a user can change their profile picture and cover photo. I am having a hard time understanding how to allow the user to upload their photos to firebase after they choose their image and click the "save button", once they click the save button I want it to upload the image and then redirect them to their regular profile view. I have read other articles and watched other tutorials but I am not able to get the result of successfully uploading both images. Here is my code so far with out any calling to upload images to firebase.

import UIKit
import Foundation
import Firebase
import FirebaseDatabase

class NewEditProfileViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var imageView1: UIImageView!
    @IBOutlet weak var imageView2: UIImageView!

    var imagePicker = UIImagePickerController()
    var imagePicked = 0

    var selectedImage1: UIImage?

    override func viewDidLoad() {
        super.viewDidLoad()



        imagePicker.delegate = self
        imagePicker.sourceType = .photoLibrary
        imagePicker.allowsEditing = true
    }


    @IBAction func chooseImage1(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            imagePicked = (sender as AnyObject).tag
            present(imagePicker, animated: true)
        }

        }

    @IBAction func chooseImage2(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerController.SourceType.photoLibrary){
            imagePicked = (sender as AnyObject).tag
                present(imagePicker, animated: true)
    }

    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage

        if imagePicked == 1 {
            imageView1.image = pickedImage
        } else if imagePicked == 2 {
            imageView2.image = pickedImage
        }
        dismiss(animated: true)
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true)
    }

@IBAction func saveButton(_ sender: Any) {
    }


    @IBAction func backButton(_ sender: Any) {
         self.dismiss(animated: true, completion: nil)
        }
    }

Thank you for the help! Like I said, Swift and Firebase overall is a new concept to me.

Allen Hinson
  • 11
  • 1
  • 2
  • 1
    Does this answer your question? [How to upload multiple image on firebase using swift?](https://stackoverflow.com/questions/49934195/how-to-upload-multiple-image-on-firebase-using-swift) – Vishal Vaghasiya Nov 16 '19 at 08:36
  • The question is a bit unclear and there is no code included that does anything with Firebase or Firebase Storage. Are you asking how to move between viewControllers or asking how to upload images to Firebase Storage? If it's the latter, the Firebase Documentation on [Uploading Image](https://firebase.google.com/docs/storage/ios/upload-files) is very helpful. Can you clarify what you're asking and take a moment to review [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) – Jay Nov 16 '19 at 15:16

1 Answers1

0

If i understood your question right i think you are asking how to upload an image to firebase apon the request of saving the image. Im hoping even though you dont have the IOS knowledge that you have the Firebase knowledge to understand the answer below .

First update your Save / upload action as below

func uploadMedia(completion: @escaping (_ url: String?) -> Void) { 
let storageRef = FIRStorage.storage().reference().child("myImage.png")
if let uploadData = UIImagePNGRepresentation(self.myImageView.image!) {
    storageRef.put(uploadData, metadata: nil) { (metadata, error) in
        if error != nil {
            print("error")
            completion(nil)
        } else {
            completion((metadata?.downloadURL()?.absoluteString)!)) 
            // your uploaded photo url.
        }
   }
}

After that all you have to do is connect to the FIRDatabase and save it to the node . Note that the nodes here are under a structure made by me so you have to adjust this method according to your node strucutre or upload your strucutre and il edit the answer .

@IBAction func addPost(_ sender: Any) {


 uploadMedia() { url in 
      guard let url = url else { return }
      ref?.child("Posts").childByAutoId().setValue([
                           "myImageURL" : url
                            ])
     }
  }

You could also reffer to this for further clarification