title: create a loading image/ activity indicator, until the image is shown in the screen in swift
body: <p>i want to show an activity indicator in the screen while the image is being downloaded from a website url then show in the images.image this is my code below. everytime i download image it print the status right away and the activity indicator never appear. been searching through the web but i still didnt understand. please help</p>
  
  <pre><code> let mygroup = DispatchGroup()
   var activityIndicator = UIActivityIndicatorView()
  
  func downloadpic(sender:UIButton){
      let catPictureURL = URL(string: addr)!
  
      // Creating a session object with the default configuration.
      // You can read more about it here https://developer.apple.com/reference/foundation/urlsessionconfiguration
      let session = URLSession(configuration: .default)
  
      // Define a download task. The download task will download the contents of the URL as a Data object and then you can do what you wish with that data.
  
      let downloadPicTask = session.dataTask(with: catPictureURL) { (data, response, error) in
          // The download has finished.
          if let e = error {
              print("Error downloading cat picture: \(e)")
          } else {
              // No errors found.
              // It would be weird if we didn't have a response, so check for that too.
              if let res = response as? HTTPURLResponse {
                  // put loading screen here
                  self.activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .white)
                  self.activityIndicator.frame = CGRect(x: 0, y: 0, width: 46, height: 46)
                  self.activityIndicator.startAnimating()
                  self.mygroup.enter()
                  print("downloading image")
                  print("Downloaded cat picture with response code \(res.statusCode)")
                  if let imageData = data {
                      // Finally convert that Data into an image and do what you wish with it.
                       self.images.image = UIImage(data: imageData)
                          self.mygroup.leave()
                          print("image already downloaded")
                          self.activityIndicator.stopAnimating()
                          self.activityIndicator.hidesWhenStopped = true
  
  
                      // Do something with your image.
                  } else {
                      print("Couldn't get image: Image is nil")
                  }
              } else {
                  print("Couldn't get response code for some reason")
              }
          }
      }
  
      downloadPicTask.resume()
  }
  </code></pre>