Swift 3 Firebase存储 - 无法上传

时间:2016-09-25 21:10:10

标签: ios swift firebase firebase-storage

我正在尝试将UIImage文件上传到firebase存储。我正在努力弄清楚为什么上传还没有完成,即使调试也是如此。

我创建了一个Firebase应用,并将存储规则设置为可公开访问。

存储参考在以下属性中定义:

var storageRef = FIRStorage.storage().reference().child("images/TestImage.png")

然后在上传方法(其中meal.photo是UIImage):

let uploadData = UIImagePNGRepresentation(meal.photo!)
let uploadTask = storageRef.put(uploadData!,metadata: nil){ metadata, error in
                if (error != nil) {
                    // Uh-oh, an error occurred!
                } else {
                    // Metadata contains file metadata such as size, content-type, and download URL.
                    let downloadURL = metadata!.downloadURL
                }
            }

我没有收到任何构建/运行时错误,并且我已确认uploadData包含我要上传的文件。但是,运行此方法时不会发生任何事情。有什么想法吗?

修改 我已将下面的整个代码段包含在内以供参考:

@IBAction func unwindToMealList(_ sender: UIStoryboardSegue) {
    if let sourceViewController = sender.source as? MealViewController, let meal = sourceViewController.meal {
        if let selectedIndexPath = tableView.indexPathForSelectedRow {
            // Update an existing meal.
            meals[(selectedIndexPath as NSIndexPath).row] = meal
            tableView.reloadRows(at: [selectedIndexPath], with: .none)
        } else {
            // Add a new meal.
            let newIndexPath = IndexPath(row: meals.count, section: 0)
            meals.append(meal)

            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "MM-dd-yyyy"
            let stringDate = dateFormatter.string(from: meal.date!)
            let mealItem = ["name": meal.name, "date": stringDate, "mealType": meal.mealType] as [String : Any]
            tableView.insertRows(at: [newIndexPath], with: .bottom)
            // Add meal to firebase database
            let mealRef = ref.child("meals")
            mealRef.setValue(mealItem)


            // Upload photo
           // let imagesRef = storageRef.child("images")
            let uploadData = UIImagePNGRepresentation(meal.photo!)
            let uploadTask = storageRef.put(uploadData!,metadata: nil){ metadata, error in
                if (error != nil) {
                    // Uh-oh, an error occurred!
                } else {
                    // Metadata contains file metadata such as size, content-type, and download URL.
                    let downloadURL = metadata!.downloadURL
                    print(downloadURL)
                }
            }
        }
        // Save the meals.
        saveMeals()
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个,我认为你把saveMeals()函数提前了,所以,没有调用上传方法......

@IBAction func unwindToMealList(_ sender: UIStoryboardSegue) {
if let sourceViewController = sender.source as? MealViewController, let meal = sourceViewController.meal {
    if let selectedIndexPath = tableView.indexPathForSelectedRow {
        // Update an existing meal.
        meals[(selectedIndexPath as NSIndexPath).row] = meal
        tableView.reloadRows(at: [selectedIndexPath], with: .none)
    } else {
        // Add a new meal.
        let newIndexPath = IndexPath(row: meals.count, section: 0)
        meals.append(meal)

        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy"
        let stringDate = dateFormatter.string(from: meal.date!)
        let mealItem = ["name": meal.name, "date": stringDate, "mealType": meal.mealType] as [String : Any]
        tableView.insertRows(at: [newIndexPath], with: .bottom)
        // Add meal to firebase database
        let mealRef = ref.child("meals")
        mealRef.setValue(mealItem)


        // Upload photo
       // let imagesRef = storageRef.child("images")
        let uploadData = UIImagePNGRepresentation(meal.photo!)
        let uploadTask = storageRef.put(uploadData!,metadata: nil){ metadata, error in
            if (error != nil) {
                // Uh-oh, an error occurred!
            } else {
                // Metadata contains file metadata such as size, content-type, and download URL.
                let downloadURL = metadata!.downloadURL
                print(downloadURL)
                // New code, put your saveMeals function here.
                self.saveMeals()
            }
        }
    }
    // Old code, please remove this saveMeals function.
    // Save the meals.
    saveMeals()
}

}