【问题标题】:Swift firebase file uploadSwift firebase 文件上传
【发布时间】:2016-06-27 16:21:09
【问题描述】:

@IBAction func shareButton(sender: AnyObject) {

    //Social Share Buttons..

        let shareActionsheet = UIAlertController(title: "Share with", message: nil, preferredStyle: .ActionSheet)
    //Facebook
    let Facebook = UIAlertAction(title: "Facebook", style: .Default){(Alert:UIAlertAction!) -> Void in
    let shareFacebook : SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        //screenShot
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(306, 441.5), false, 0)
        self.view.drawViewHierarchyInRect(CGRectMake(-35, -35, self.view.bounds.size.width, self.view.bounds.size.height), afterScreenUpdates: true)
        let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
        //screenShot finish

//创建截图图像
//shareFacebook.addImage(图片)

        self.presentViewController(shareFacebook, animated: true, completion: nil)


        if let user = FIRAuth.auth()?.currentUser{



        //dosya upload - servise referans sağla..
        let storage = FIRStorage.storage()

        //referans yolu
        let storageRef = storage.referenceForURL("gs://creawo-80503.appspot.com")

        let data: NSData = NSData()
        // Create a reference to the file you want to upload
        let riversRef = storageRef.child("\(self.user!.uid+"/share_pic.jpg")/")

        // Upload the file to the path "images/rivers.jpg"
        let uploadTask = riversRef.putData(data, metadata: nil) { metadata, error in
            if (error != nil) {
                // Uh-oh, an error occurred!
            } else {

我展示了路径和文件创建。让警报在图像中截取屏幕截图。我想安装firebase // 元数据包含文件元数据,例如大小、内容类型和下载 URL。

I couldn't find a solution about this place        
let downloadURL = metadata!.downloadURL()
            }
        }

}

【问题讨论】:

    标签: ios swift firebase


    【解决方案1】:
    @IBAction func postAd(sender: AnyObject) {
        self.name = self.textField.text!
        self.category = (self.chooseButton.titleLabel?.text)!
        self.subcat = self.subcategory.text!
    
        if (self.name != "" && self.category != "" && self.subcat != ""){
    
            self.price = Int(self.priceField.text!)!
            self.transaction = (self.transactionButton.titleLabel?.text
            )!
    
            let book = ["productId": self.productId,
                        "name": self.name,
                        "parentId": self.parentId,
                        "price": self.price,
                        "transaction":self.transaction,
                        "category":self.category,
                        "subcat":self.subcat]
            let childUpdates = ["products/\(productId)": book]
    
    
            let data: NSData = UIImageJPEGRepresentation(imagePreview.image!, 0.8)!
    
            // Create a reference to the file you want to upload
            let metadata = FIRStorageMetadata()
            metadata.contentType = "image/jpeg"
            // Upload the file to the path "images/rivers.jpg"
            let uploadTask = storageRef.child("\(productId)").putData(data, metadata: nil) { metadata, error in
                if (error != nil) {
                    // Uh-oh, an error occurred!
                    print("Error")
                } else {
                    print("Successfully posted")
                    // Metadata contains file metadata such as size, content-type, and download URL.
                    let downloadURL = metadata!.downloadURL()
                    print(downloadURL)
                }
            }
            print(uploadTask)
    
    
            self.ref.updateChildValues(childUpdates)
    
            self.performSegueWithIdentifier("productToHome", sender: self)
        }
        else{
            let alert:UIAlertController = UIAlertController(title: "Fill all details", message: "Please Enter all details", preferredStyle: UIAlertControllerStyle.Alert)
            alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
            self.presentViewController(alert, animated: true, completion: nil)
            print("Fill all the details")
        }
    }
    

    我正在将这本书的详细信息上传到我的数据库中。 您可以像我一样使用“元数据”部分。 这里的 'imagePreview' 包含从 imagePicker 中选取的图像。下面给出了选择图像的功能。

    //Internal function to select image and preview it in Image View
    // MARK: - UIImagePickerControllerDelegate Methods
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo editingInfo: [String : AnyObject]) {
        if let pickedImage = editingInfo[UIImagePickerControllerOriginalImage] as? UIImage {
            imagePreview.contentMode = .ScaleAspectFit
            imagePreview.image = pickedImage
        }
        dismissViewControllerAnimated(true, completion: nil)
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }
    //Image Viewer Ends
    

    【讨论】:

    • 谢谢巴拉特 :) +100
    • @OğuzMuslu 避免在回复中表示感谢。如果它解决了您的问题,请将答案设置为正确。
    猜你喜欢
    • 2017-10-14
    • 2020-12-10
    • 2019-12-02
    • 2021-11-20
    • 1970-01-01
    • 2018-11-11
    • 2014-12-15
    • 2017-10-16
    • 2019-03-08
    相关资源
    最近更新 更多