Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 4.36 KB | None | 0 0
  1. @IBAction func postPressed(_ sender: Any) {
  2.         AppDelegate.instance().showActivityIndicator()
  3.         func upload(_ image: UIImage, completion: @escaping (String) -> Void) {
  4.             let storage = Storage.storage().reference(forURL:               "gs://mobile-d9fcd.appspot.com")
  5.             let uid = Auth.auth().currentUser!.uid
  6.             let key = self.ref.child("posts").childByAutoId().key
  7.             let imageRef = storage.child("posts").child(uid).child("\(key).jpg")
  8.             let data = UIImageJPEGRepresentation(self.previewImage.image!, 0.6)
  9.  
  10.             let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata, error) in
  11.                 if error != nil {
  12.                     print(error!.localizedDescription)
  13.                     AppDelegate.instance().dismissActivityIndicator()
  14.                     return
  15.                 }
  16.                 imageRef.downloadURL(completion: { (url, error) in
  17.                     if let url = url {
  18.                         let feed = ["userID" : uid,
  19.                                     "pathToImage" : url.absoluteString,
  20.                                     "likes" : 0,
  21.                                     "author" : Auth.auth().currentUser!.displayName!,
  22.                                     "postID" : key] as [String : Any]
  23.                        
  24.                         let postFeed = ["\(key)" : feed]
  25.                         self.ref.child("posts").updateChildValues(postFeed)
  26.                     }
  27.                 })
  28.             }
  29.             uploadTask.resume()
  30.         }
  31.        
  32.         func uploadSubPosts(_ image: UIImage, completion: @escaping (String) -> Void) {
  33.             let storage = Storage.storage().reference(forURL:               "gs://mobile-d9fcd.appspot.com")
  34.             let uid = Auth.auth().currentUser!.uid
  35.             let key = self.ref.child("posts").childByAutoId().key
  36.             let key2 = self.ref.child("subposts").childByAutoId().key
  37.             let imageRef = storage.child("subposts").child(uid).child("\(key2).jpg")
  38.            
  39.             var subpostsDictionary = [String: String]()
  40.             for imagePath in self.imagePathArray {
  41.                 subpostsDictionary[UUID().uuidString] = imagePath
  42.             }
  43.            
  44.             let data = UIImageJPEGRepresentation(image, 0.6)
  45.            
  46.             let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata, error) in
  47.                 if error != nil {
  48.                     print(error!.localizedDescription)
  49.                     AppDelegate.instance().dismissActivityIndicator()
  50.                     return
  51.                 }
  52.                 imageRef.downloadURL(completion: { (url1, downloadError) in
  53.                     //check for errors
  54.                     if let unwrappedDownloadError = downloadError {
  55.                         print(unwrappedDownloadError)
  56.                        
  57.                         // unwrap the url
  58.                     } else if let unwrappedUrl = url1 {
  59.                         // add the url to the local array that will be used to create your post object
  60.                         self.imagePathArray.append(unwrappedUrl.absoluteString)
  61.                        
  62.                         let feed = ["postId": key,
  63.                                     "subposts": subpostsDictionary] as [String : Any]
  64.                        
  65.                         let postFeed = ["\(key2)" : feed]
  66.                         self.ref.child("subposts").updateChildValues(postFeed)
  67.                         self.picker.dismiss(animated: true, completion: nil)
  68.                        
  69.                     }
  70.                 })
  71.             }
  72.             uploadTask.resume()
  73.         }
  74.        
  75.         var imagePaths = [String]()
  76.         func bulkUpload(_ images: [UIImage], completion: @escaping ([String]) -> Void) {
  77.             var counter = 0
  78.             for image in images {
  79.                 uploadSubPosts(image) { (urlPath) in
  80.                     imagePaths.append(urlPath)
  81.                     counter += 1
  82.                     if counter == images.count {
  83.                         completion(imagePaths)
  84.                     }
  85.                 }
  86.             }
  87.         }
  88.  
  89.         bulkUpload(self.subpostsArray) { [weak self] (urlPaths) in
  90.             let subPost = SubPost(pathToImage: imagePaths)
  91.         }
  92.         AppDelegate.instance().dismissActivityIndicator()
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement