[英]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我正在使用 firebase 上传一些图片,它工作正常,但问题是我无法获取图片的下载 url
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 ??当我在上传方法中打印 strURL 时,url 来了,但是当我在 btnUploadImgsAction 中打印时,它没有来,我有什么错?? 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). 使用“ completion(strURL)”有助于成功地从其功能中检索strURL,但是当我尝试使用putData将strURL保存在数据库中时,数据库中的任何条目都不会被保存(“ url?.absoluteString”的值为“可选” (“ https:// firebasestorage ...”)',并在Firebase数据库中保留nil值)。 There is no problem with saving on Firebase Storage anyway.
无论如何,保存Firebase存储没有问题。 Is there a way to force unwrap "url?.absoluteString" so it will return a String value to strURL ?!
有没有一种方法可以强制解开“ url?.absoluteString”,以便它将String值返回给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)")
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.