我想从Firebase Storage iOS Swift获取图像的下载URL



我正在使用firebase上传一些图像,它的工作正常,但是我无法获得图像的下载URL

在这里我的代码

@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
}

当我在上传方法内打印弹奏时,url出现了,但是当我在btnuploadimgsaction中打印时,它没有来了,唐尼格怎么了?请提前帮助

您应该创建一个直接返回字符串的闭合方法,因为此方法是async方法

您的方法声明应该像

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

用您的方法更新

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)
                }
            })
        })
    }
}

和功能调用将为

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

相关内容

  • 没有找到相关文章

最新更新