I'm trying to upload an image to Firebase storage using following code:
let storageRef = FIRStorage.storage().reference()
let imageRef = storageRef.child("AAA").child("BBB").child("CCC.jpeg")
let metaData = FIRStorageMetadata()
metaData.contentType = "image/jpeg"
if let uploadData = UIImageJPEGRepresentation(sampleImage, 0.5){
imageRef.put(uploadData, metadata: metaData, completion: { (metaData, error) in
print("finished")
if error != nil{
print(error!.localizedDescription)
}else{
print("success")
}
})
}else{
print("Cannot convert image to JPEG format")
}
The problem is that The above code doesn't do anything, in other words, nothing prints out. I have tried uploading strings to Firebase database, and it succeed. I've also checked the rules of Firebase storage:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read: if true;
allow write: if request.auth != null;
}
}
}
So, what did i do wrong? Is there anything that I need to set up? Please help!!
Disclaimer: Let's just say my middle name isn't SwiftLint.
Try this, and make sure your database rules are similar as well. You should get a link in your database that will point you to the image in storage.
if let theData = UIImageJPEGRepresentation(sampleImage, 0.5) {
self.helper(photoData:theData)
}
// ^^ this can happen elsewhere ^^
func upload(photoData:Data) {
let imgName = Int(round(Date().timeIntervalSince1970)
let metadata = FIRStorageMetadata()
metadata.contentType = "image/jpeg"
let imageRef = FIRStorage.storage().reference().child("AAA/BBB/\(imgName).jpg")
imageRef.put(photoData, metadata: metadata) { (metadata, error) in
if let error = error {
print("Error uploading: \(error)")
return
}
let data:[String:Any] = [
"timeStamp":"\(Date())",
"image":metadata!.downloadURL()!.absoluteString
]
let dbRef = FIRDatabase.database().reference()
dbRef.child("imageUploads").childByAutoId().setValue(data)
}
}
Well, both of my original solution and #MGTLA solution are correct, the problem turns out to be the while loop after uploading image...which is unrelated to this topic.
Related
I can only upload a single photo to FirebaseStorage.
All the tutorials I have seen on youtube use FirebaseStorage to save user's profile pictures. However, I am trying to make a restaurant app where I should be able to keep several pictures and not only one. My code right now only allows me to keep one. As soon as I try to upload a second image, it erases the first one.
Here's my code:
#IBAction func agregarComidaFav(_ sender: Any) {
guard let imageData = imagen?.jpegData(compressionQuality: 0.4) else {
return
}
let storageRef = Storage.storage().reference(forURL: "gs://comidas-68043.appspot.com")
let storageProfileRef = storageRef.child("ComidasFavoritas")
let metadata = StorageMetadata()
metadata.contentType = "image/png"
storageRef.child("comidasFavoritas/png").putData(imageData, metadata: metadata) { (storageMetaData, error) in
if error != nil {
print(error?.localizedDescription ?? "")
return
}
}
storageProfileRef.downloadURL(completion: { (url, error) in
if let metaImageUrl = url?.absoluteString {
print(metaImageUrl)
self.imagenURL = metaImageUrl
}
})
var ref: DocumentReference? = nil
ref = db.collection("comidasFavoritas").addDocument(data: [
"imagenURL" : imagenURL
]) { err in
if let err = err {
print("Error agregando comida: \(err)")
} else {
let id = ref!.documentID
print("Comida agregado con ID: \(id)")
}
}
}
This line:
storageRef.child("comidasFavoritas/png").putData(...)
...guarantees that each time, the data is going to be written to "comidasFavoritas/png"
I don't know anything about how you're keeping track of your data/images in your app, but you'll probably need some way to keep track of an ID for that image. Then, when you store it, you could store it like:
storageRef.child("comidasFavoritas/png\(imageId)").putData(...)
You'd probably also want to store that ID in your database when you store the image URL.
I am trying to load an image to an imageView from firebase storage as expected.
I have the below function that is called in viewDidLoad but not sure why it's not working.
I copied the urlName.com directly from the image within Firebase storage.
func setProfileImage() {
let currentUserID = Auth.auth().currentUser!.uid
Storage.storage().reference(forURL: "gs://urlName.com").child("Folder/\(currentUserID).jpg").getData(maxSize: 1048576, completion: { (data, error) in
guard let imageData = data, error == nil else {
return
}
self.profileImage.image = UIImage(data: imageData)
})
}
This code doesn't throw an error but also doesn't load anything.
Any help much appreciated
Thanks
1- Allow Access for development mode, In Storage > Rules:
service firebase.storage {
match /b/yourapp.appspot.com/o {
match /{allPaths=**} {
// Allow access by all users
allow read, write;
}
}
}
Get the data:
let reference = Storage.storage().reference(withPath: "your_folder_path)/image.jpg")
reference.getData(maxSize: (1 * 1024 * 1024)) { (data, error) in
if let _error = error{
print(_error)
} else {
if let _data = data {
let myImage:UIImage! = UIImage(data: _data)
self.profileImage.image = myImage
}
}
}
I have an image that is uploaded to the Firebase Storage but I can not get the downloadURL of this to save it in my database.
I already read a lot of posts on StackOverflow but there was nothing working out for me. The image is loaded into the storage but the error says that it is not existing.
let image = self.selectedImage
let imageData = UIImageJPEGRepresentation(image, 0.1)
let storageRef = Storage.storage().reference().child(uid)
storageRef.putData(imageData, metadata: nil) // i know that i can use a completion here but i left it for now
storageRef.downloadURL { url, error in
if let error = error {
print(error)
} else {
// do sth. else
}
}
These are the errors that I get:
FIRStorageErrorDomain Code=-13010 "Object W002MjRvi0d8JfVwImUJhH0ph2O2 does not exist."UserInfo={object=W002MjRvi0d8JfVwImUJhH0ph2O2,
ResponseBody={
"error": {
"code": 404,
"message": "Not Found. Could not get object"
}
}
ResponseErrorDomain=com.google.HTTPStatus, ResponseErrorCode=404}
Please check Storage Rules
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth!=null;
}
}
}
In Your ViewController
import Firebase
import FirebaseAuth
import ImageIO
var imgData: NSData = NSData(data: UIImageJPEGRepresentation((self.img_Photo?.image)!, 0.5)!)
self.uploadProfileImageToFirebase(data: imgData)
func uploadProfileImageToFirebase(data:NSData){
guard let userID = Auth.auth().currentUser?.uid else {
return
}
let storageRef = Storage.storage().reference().child(“Images”).child(userID)
if data != nil {
storageRef.putData(data as Data, metadata: nil, completion: { (metadata, error) in
if(error != nil){
print(error)
return
}
// Fetch the download URL
storageRef.downloadURL { url, error in
if let error = error {
// Handle any errors
if(error != nil){
print(error)
return
}
} else {
// Get the download URL for 'images/stars.jpg'
let urlStr:String = (url?.absoluteString) ?? ""
}
}
})
}
}
I try to upload multiple images from library to Firebase storage. The upload process works perfectly, all the images is on Firebase storage but when i try to download the image url link to save in database, it shows nil. Please help me how to fix this? Thank a lot
for imageData in ArrayMedia {
print(imageData)
let filePath = "\(currentUserId)/\(key)"
let data = UIImageJPEGRepresentation(imageData, 0.3)
let metadata = StorageMetadata()
metadata.contentType = "image/jpg"
storageReference.child(filePath).putData(data!, metadata: metadata, completion: { (metaData, error) in
print(metadata)
if error != nil {
print(error?.localizedDescription as Any)
} else {
let PhotoUrl = metadata.downloadURL()?.absoluteString // GOT NIL IN THIS LINE
print(PhotoUrl)
let value = ["Username" : currentUserName, "UserId" : currentUserId, "Text" : self.tvPost.text] as [String : Any]
databaseReference.child("PendingPost").child(currentUserId).child(key).updateChildValues(value)
databaseReference.child("PendingPost").child(currentUserId).child(key).child("Images").child(key).setValue(["PhotoUrl": PhotoUrl])
}
self.showAlert()
})
}
Try to use this instead :
storageRef.child(path).putData(data, metadata: metaData){ (metaData,error) in
if let error = error {
print(error.localizedDescription)
return
} else {
let downloadURL = metaData!.downloadURL()!.absoluteString
}
}
It works for me.
I'm trying to create a function that uploads images to Firebase Storage and returns the download url for their path so I can use it other parts of the app.
This is what the function looks like:
func uploadImage(to reference:StorageReference, image:UIImage) -> URL? {
let imageData = UIImageJPEGRepresentation(image, 0.2)
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
var downloadURL = metadata.downloadURL()
reference.putData(imageData!, metadata: metadata) { (metadata, error) in
if error != nil {
print("Couldnt upload due to \(String(describing: error))")
}
downloadURL = metadata?.downloadURL()
}
return downloadURL!
}
I can't seem to get the result that I want as downloadUrl always returns nil. What am I doing wrong?
The problem here is that your function is returning before the upload is complete. In other words your function needs to return a callback, rather than a plain URL. Something like -
func uploadImage(to reference:StorageReference, image:UIImage, completion: #escaping (URL?) -> Void) {
let imageData = UIImageJPEGRepresentation(image, 0.2)
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
var downloadURL = metadata.downloadURL()
reference.putData(imageData!, metadata: metadata) { (metadata, error) in
if error != nil {
print("Couldnt upload due to \(String(describing: error))")
completion(nil)
} else {
if let downloadUrl = metadata?.downloadURL() {
completion(downloadUrl)
} else {
completion(nil)
}
}
}
}