Firebase Storage Warning: downloadURL()' is deprecated: Use `StorageReference.downloadURLWithCompletion()
Basically not using the metadata but instead just getting the url after the success of your observe event. Since it's successful and you know it's there, you can download the URL. It's there in their docs to 'Generate a download URL'. Below, I'm assuming your StorageReference is uploadProfilePicTask.
uploadProfilePicTask.downloadURL(completion: { (url, error) in
if (error == nil) {
if let downloadUrl = url {
// Make you download string
let downloadString = downloadUrl.absoluteString
}
} else {
// Do something if error
}
})
downloadURL()' is deprecated: Use `StorageReference.downloadURLWithCompletion()` to obtain a current download URL.?
The error says that you need to use StorageReference.downloadURLWithCompletion()
well you need to use it:
let storageItem = Storage.storage().reference().child(ImageUid)
storageItem.putData(ImageData, metadata: metadata) { (metadata, error) in
if error != nil {
print("Couldn't Upload Image")
} else {
print("Uploaded")
storageItem.downloadURL(completion: { (url, error) in
if error != nil {
print(error!)
return
}
if url != nil {
self.SetUpUser(Image: url!.absoluteString)
}
}
}
}
How to use StorageReference.downloadURLWithCompletion() in Firebase?
You can check the updated documentation of Firebase 5 upload files here, the API changed so you should use StorageReference's downloadURL method.
So your code become something like this:
func signupEvent(){
Auth.auth().createUser(withEmail: email.text!, password: password.text!){(user,err) in
let uid = user?.user.uid
let image = UIImageJPEGRepresentation(self.imageView.image!, 0.1)
let storageItem = Storage.storage().reference().child("userImage").child(uid!)
storageItem.putData(image!, metadata:nil, completion:{(data, error) in
// Now is good to check errors
if error != nil {
print("Error: couldn't upload the image")
}
else {
storageItem.downloadURL(completion: { (url, error) in
if error != nil {
print(error!)
return
}
if url != nil {
let imageUrl = url!.absoluteString
Database.database().reference().child("users").child(uid).setValue(["name": self.name.text!, "profileImageUrl" : imageUrl])
}
}
}
})
}
}
Update for metadata?.downloadURL().absoluteString that returns a String
The metadata.downloadURL
no longer exists (and hasn't existed in SDKs released since May 2018). To get the download URL you now have to call downloadURL()
on the storage reference and then use the URL in the callback.
So the place to put your own code that needs the download URL is right where it now says //Do something with url
in the code you got from the documentation. If you want to pass that URL to your `` method, that'd looks something like:
storageRef.putData(imageData, metadata: nil, completion: {(metadata, error) in
if error != nil {
ProgressHUD.showError(error!.localizedDescription)
return
}
//*********** Need to fix this **************
let photoUrl = storageRef.downloadURL(completion: {url, error in
if error != nil {
print("Failed to download url:", error!)
return
} else {
self.sendDatatoDatabase(photoUrl: url)
}
})
})
Firebase storage downloadUrl completion handler always returns error
Okay, so I was able to fix that and get the image path in the completion handler. What was wrong? It is in these two lines:
imageReference.putData(imageData, metadata: metadata, completion: { [storage] (metadata, error) in
storage.downloadURL(completion: { (url, error) in
imageReference is a reference to the image itself and storage is a reference to the global storage. This misunderstanding came from the docs. So the way it should be:
imageReference.putData(imageData, metadata: metadata, completion: { (metadata, error) in
imageReference.downloadURL(completion: { (url, error) in
Retrieve Firebase storage image without getting downloadurl flutter
If you're using the FlutterFire Storage library in your app, you can call getData
on a reference to the file to get its data. So with that you just need to know the path to the data, and you won't need the download URL in your application. Once you have the data locally, you can create an image out of it with: Converting a byte array to image in Flutter?
Unlike download URLs, the call to getData()
is checked by security rules, so you'll have to ensure that the user is permitted to access the file.
Kingfisher: Ambiguous use of 'setBackgroundImage(with:for:placeholder:options:progressBlock:completionHandler:)'
That error is because you need to handle the completionHandler
instead of passing nil
. Try below code:
button.kf.setBackgroundImage(with: URL(string: picture), for: .normal, placeholder: nil, options: options, progressBlock: nil) { result in
// result is either a .success(RetrieveImageResult) or a .failure(KingfisherError)
switch result {
case .success(let value):
// The image was set to image view:
print(value.image)
// From where the image was retrieved:
// - .none - Just downloaded.
// - .memory - Got from memory cache.
// - .disk - Got from disk cache.
print(value.cacheType)
// The source object which contains information like `url`.
print(value.source)
case .failure(let error):
print(error) // The error happens
}
}
Related Topics
How to Load Image in Swift Using Alamofire
How to Create Enum with Raw Type of Cgpoint
How to Track More Than 4 Images at a Time with Arkit
Global Function Sequence(State:Next:) and Type Inference
How to Add an Optional String Extension
How to Set Addobserver in Swiftui
How to Detect Vertical Planes in Arkit
Implicitly Lazy Static Members in Swift
Block Scroll Down in Scrollview - Swiftui
Fibonacci Calculator Stack Overflows at 93Nd Number in Swift
How to Upload Zip Data with Alamofire
How to Avoid Nested Navigation Bars in Swiftui
Implicit Return in a Closure Causing an Error
Segue from a Slpagingviewswift Vc and Dismiss the Destination Vc to Return
Convert String to Bool in Swift - via API or Most Swift-Like Approach