所以我制作了一个社交网络应用,我希望这样做,以便当用户创建新帖子时,在帖子中添加图片是可选的,但标题是必需的。
我正在使用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)
添加保护声明以检查您的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!)
})
}
}