Advertisement
Guest User

Untitled

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