Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @IBAction func postPressed(_ sender: Any) {
- AppDelegate.instance().showActivityIndicator()
- func upload(_ image: UIImage, completion: @escaping (String) -> Void) {
- let storage = Storage.storage().reference(forURL: "gs://mobile-d9fcd.appspot.com")
- let uid = Auth.auth().currentUser!.uid
- let key = self.ref.child("posts").childByAutoId().key
- let imageRef = storage.child("posts").child(uid).child("\(key).jpg")
- let data = UIImageJPEGRepresentation(self.previewImage.image!, 0.6)
- let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata, error) in
- if error != nil {
- print(error!.localizedDescription)
- AppDelegate.instance().dismissActivityIndicator()
- return
- }
- imageRef.downloadURL(completion: { (url, error) in
- if let url = url {
- let feed = ["userID" : uid,
- "pathToImage" : url.absoluteString,
- "likes" : 0,
- "author" : Auth.auth().currentUser!.displayName!,
- "postID" : key] as [String : Any]
- let postFeed = ["\(key)" : feed]
- self.ref.child("posts").updateChildValues(postFeed)
- }
- })
- }
- uploadTask.resume()
- }
- func uploadSubPosts(_ image: UIImage, completion: @escaping (String) -> Void) {
- let storage = Storage.storage().reference(forURL: "gs://mobile-d9fcd.appspot.com")
- let uid = Auth.auth().currentUser!.uid
- let key = self.ref.child("posts").childByAutoId().key
- let key2 = self.ref.child("subposts").childByAutoId().key
- let imageRef = storage.child("subposts").child(uid).child("\(key2).jpg")
- var subpostsDictionary = [String: String]()
- for imagePath in self.imagePathArray {
- subpostsDictionary[UUID().uuidString] = imagePath
- }
- let data = UIImageJPEGRepresentation(image, 0.6)
- let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata, error) in
- if error != nil {
- print(error!.localizedDescription)
- AppDelegate.instance().dismissActivityIndicator()
- return
- }
- imageRef.downloadURL(completion: { (url1, downloadError) in
- //check for errors
- if let unwrappedDownloadError = downloadError {
- print(unwrappedDownloadError)
- // unwrap the url
- } else if let unwrappedUrl = url1 {
- // add the url to the local array that will be used to create your post object
- self.imagePathArray.append(unwrappedUrl.absoluteString)
- let feed = ["postId": key,
- "subposts": subpostsDictionary] as [String : Any]
- let postFeed = ["\(key2)" : feed]
- self.ref.child("subposts").updateChildValues(postFeed)
- self.picker.dismiss(animated: true, completion: nil)
- }
- })
- }
- uploadTask.resume()
- }
- var imagePaths = [String]()
- func bulkUpload(_ images: [UIImage], completion: @escaping ([String]) -> Void) {
- var counter = 0
- for image in images {
- uploadSubPosts(image) { (urlPath) in
- imagePaths.append(urlPath)
- counter += 1
- if counter == images.count {
- completion(imagePaths)
- }
- }
- }
- }
- bulkUpload(self.subpostsArray) { [weak self] (urlPaths) in
- let subPost = SubPost(pathToImage: imagePaths)
- }
- AppDelegate.instance().dismissActivityIndicator()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement