2017-06-22 37 views
0

因此,我正在製作一個社交網絡應用程序,我希望這樣做是爲了當用戶創建新帖子時,添加一個圖片到他們的帖子是可選的,但標題是必需的。製作一個應用程序,我想使照片可選,但製作新帖子時需要標題

我正在使用Swift 3和Xcode 8.2.1。我還將Google Firebase用作我的移動後端即服務(MBAAS)。

下面是我的代碼:

import UIKit 
import FirebaseStorage 
import Firebase 
import FirebaseDatabase 

class NewPostViewController: UIViewController { 
    @IBOutlet weak var photo: UIImageView! 
    @IBOutlet weak var caption: UITextView! 
    @IBOutlet weak var postButton: UIButton! 

var selectedImage: UIImage? 



override func viewDidLoad() { 
    super.viewDidLoad() 

    let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.handleSelectPhoto)) 
    photo.addGestureRecognizer(tapGesture) 
    photo.isUserInteractionEnabled = true 

} 

func handleSelectPhoto() { 
    let pickerController = UIImagePickerController() 
    pickerController.delegate = self 
    present(pickerController, animated: true, completion: nil) 
} 

@IBAction func postButton(_ sender: Any) { 
    ProgressHUD.show("Loading...", interaction: false) 
    if let postImage = self.selectedImage, let imageData = UIImageJPEGRepresentation(postImage, 0.1) { 
     let photoID = NSUUID().uuidString 
     let storageRef = Storage.storage().reference(forURL: "gs://bruh-b7907.appspot.com/").child("post_photos").child(photoID) 
     storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in 
      if error != nil { 
       ProgressHUD.showError("Could not post") 
       return 
      } 

      let postImageUrl = metadata?.downloadURL()?.absoluteString 
      self.sendDataToDatabase(photoUrl: postImageUrl!) 

     }) 
    } 

} 

func sendDataToDatabase(photoUrl: String) { 
    let ref = Database.database().reference() 
    let postsReference = ref.child("posts") 
    let newPostId = postsReference.childByAutoId().key 
    let newPostReference = postsReference.child(newPostId) 
    newPostReference.setValue(["postPhotoUrl": photoUrl, "caption": caption.text!], withCompletionBlock: { 
     (error,ref) in 
     if error != nil { 
      ProgressHUD.showError("Could not post") 
      return 
     } 
     ProgressHUD.showSuccess("Successfully Posted") 
    }) 
} 

} 
extension NewPostViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate { 
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { 
     if let image = info["UIImagePickerControllerOriginalImage"] as? UIImage{ 
      selectedImage = image 
      photo.image = image 
     } 
     dismiss(animated: true, completion: nil) 
    } 
} 
+0

你的意思是必需的字幕時,他們張貼相片或每一次? –

+0

無論是否有照片,標題都是必需的 –

回答

0

添加後衛語句來檢查你的caption.text

@IBAction func postButton(_ sender: Any) { 
    ProgressHUD.show("Loading...", interaction: false) 
    if let postImage = self.selectedImage, let imageData = UIImageJPEGRepresentation(postImage, 0.1) { 
     let photoID = NSUUID().uuidString 
     let storageRef = Storage.storage().reference(forURL: "gs://bruh-b7907.appspot.com/").child("post_photos").child(photoID) 
     storageRef.putData(imageData, metadata: nil, completion: { (metadata, error) in 
      if error != nil { 
       ProgressHUD.showError("Could not post") 
       return 
      } 

      // if there's no any text in your UITextView it'll return 
      guard let requiredCaption = caption.text else { 
       print("No caption to submit") 
       return 
      } 

      let postImageUrl = metadata?.downloadURL()?.absoluteString 
      self.sendDataToDatabase(photoUrl: postImageUrl!) 

     }) 
    } 

} 
相關問題