简体   繁体   中英

I want to get the download url of image from firebase storage ios swift

I am using firebase to upload some images, and its work fine, but the problem I can not get the download url of the image

here my code

@IBAction func btnUploadImgsAction(_ sender: UIButton) {


    let url1 = uploading(img: Img1)
    print("///////////img 1   //////// \(url1)   ////////")
    let url2 = uploading(img: Img2)
    print("///////////img 2   //////// \(url2)   ////////")
    let url3 = uploading(img: Img3)
    print("///////////img 3   //////// \(url3)   ////////")
    let url4 = uploading(img: Img4)
    print("///////////img 4   //////// \(url4)   ////////")


}


func uploading( img : UIImageView)-> String{
    var strURL = ""
    let imageName = NSUUID().uuidString
    let storeImage = self.storageRef.child("profile_Images").child(imageName)

    if let uploadImageData = UIImagePNGRepresentation((img.image)!){
        storeImage.putData(uploadImageData, metadata: nil, completion: { (metaData, error) in
            storeImage.downloadURL(completion: { (url, error) in
                if let urlText = url?.absoluteString {

                     strURL = urlText
                    print("///////////tttttttt//////// \(strURL)   ////////")


                }
            })
        })


    }

    return strURL
}

when I print the strURL inside the uploading method the url comes but when I printed in the btnUploadImgsAction its not coming, What am donig wrong ?? please help thanks in advance

You should create a closure method instead method which directly returns string because this method is async method

Your method declaration should be something like below

func uploading( img : UIImageView, success: (url: String) -> Void) {
 // return with clouser
 success(url: strurl)
}

Updated with your method

func uploading( img : UIImageView, completion: @escaping ((String) -> Void)) {
    var strURL = ""
    let imageName = NSUUID().uuidString
    let storeImage = self.storageRef.child("profile_Images").child(imageName)

    if let uploadImageData = UIImagePNGRepresentation((img.image)!){
        storeImage.putData(uploadImageData, metadata: nil, completion: { (metaData, error) in
            storeImage.downloadURL(completion: { (url, error) in
                if let urlText = url?.absoluteString {

                    strURL = urlText
                    print("///////////tttttttt//////// \(strURL)   ////////")

                    completion(strURL)
                }
            })
        })
    }
}

And function call would be

let str = uploading(img: imageObject) { (url) in
     print(url)
 }

Using "completion(strURL)" helped to successfully retrieve strURL out of its function, but yet when I try to save strURL in Database using putData, no entry in database will be saved ("url?.absoluteString" has a value of 'Optional(" https://firebasestorage ...")' and hold a nil value in Firebase Database). There is no problem with saving on Firebase Storage anyway. Is there a way to force unwrap "url?.absoluteString" so it will return a String value to strURL ?! I appreciate a lot if someone help me!!!

My code is like this:

     newImageRef.putData(imageData, metadata: nil) { (metadata, error) in       
            newImageRef.downloadURL(completion: {(url, error) in
                if let downloadURL = url?.absoluteString , error == nil {
                    self.imageDownloadURL = downloadURL
                    completion(self.imageDownloadURL)
                    print("Image URL is: \(self.imageDownloadURL)")
                }
            })
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM