I'm getting an error with firebase and my code.
Type 'StorageReference' has no member 'put'
I tried putData and still nothing. what is the best solution to fix this problem?
I'm also getting this error when users tap to pick a profile picture
'UIImageJPEGRepresentation' has been replaced by instance method
'UIImage.jpegData(compressionQuality:)'
StorageReference.put(imageData, metadata: nil, completion: { (metadata, error) in
if let profileImg = self.selectedImage, let imageData = UIImageJPEGRepresentation(profileImg, 0.1)
theres a article how to upload data to storage on firebase:
https://firebase.google.com/docs/storage/ios/upload-files
// Upload the file to the path "images/rivers.jpg"
let uploadTask = riversRef.putData(data, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
let size = metadata.size
// You can also access to download URL after upload.
riversRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
return
}
}
}
Related
I have a video creating process where a user selects a video using UIImagePickerController. After selecting a video, the url of the video is passed to the video details page. Once the user completes the details and clicks upload, I run this code:
if canProceed(){
let fileName = "\(UUID().uuidString).mov"
storage.reference(withPath: "videos/\(Auth.auth().currentUser?.email ?? "")/\(fileName)").putFile(from: vidURL, metadata: nil) { metadata, err in
if err != nil{
print(err)
return
}
metadata?.storageReference?.downloadURL(completion: { url, err in
if err != nil{
print(err)
return
}
db.collection("reelPool").document(fileName).setData(["url": url])
})
}
}
Nothing is uploaded to cloud storage and the return is:
Error Domain=FIRStorageErrorDomain Code=-13000 "An unknown error occurred, please check the server response."
HI I think Here is your answer Just try with it ,
// Url is Video Url Which you will get when Pick a video from Image Picker
func upload(file: URL, completion: #escaping ((_ url : URL?) -> ())) {
let name = "\(yourFile Name)).mp4"
do {
let data = try Data(contentsOf: file)
let storageRef =
Storage.storage().reference().child("Videos").child(name)
if let uploadData = data as Data? {
let metaData = StorageMetadata()
metaData.contentType = "video/mp4"
storageRef.putData(uploadData, metadata: metaData
, completion: { (metadata, error) in
if let error = error {
completion(nil)
}
else{
storageRef.downloadURL { (url, error) in
guard let downloadURL = url else {
completion(nil)
return
}
completion(downloadURL)
}
print("success")
}
})
}
}catch let error {
print(error.localizedDescription)
}
And then You will get Url of the Video, now run code of firestore and Pass Url in dictionary .
Did you remember to edit your Firestore rules to allow for read/write access? Your database may not allow writing or only allow writing for authenticated users. Typically Code -13000 is related to permissioning issues on the database.
Using Swift, Firebase, and Xcode 11, I am starting to create a social media app. While creating the sign in page's image-icon-upload thingy, after I run my code, insert the email/username/password, and enter the signIn/signUp button, I get this error:
nw_connection_receive_internal_block_invoke [C1] Receive reply failed with error "Operation canceled"
Here is my view controller code inside my signIn/signUp button's image part of the #IBAction:
//Image Upload
var downloadURL: URL!
if let imageData = self.selectedImage.jpegData(compressionQuality: 0.2) {
let metaData = StorageMetadata()
Storage.storage().reference().putData(imageData, metadata: metaData) { (metadata, error) in
guard let metadata = metadata else {
// Uh-oh, an error occurred!
return
}
// Metadata contains file metadata such as size, content-type.
let size = metadata.size
self.storageRef.downloadURL { (url, error) in
downloadURL = url
}
Database.database().reference().child("user").child((user?.user.uid)!).setValue([
"username": self.usernameField.text,
"userImg": downloadURL
])
}
}
Any ideas?
I have an app where you can upload videos to firebase. The problem I have recently encountered is that all videos uploaded to firebase are 0 sec long (This is when in the DB, before, as in in the app, they are of correct length), which is of course wrong.
Some things I've tried:
I check how I upload which i also included bellow and it seems correct.
thumbnail is of type UIImage
The video right before I send it (in the preview I have in-app) the video looks perfect.
Another thing I noticed is that a thumbnail I upload with the video which is an image is being uploaded as a video.
} else if let vidData = media.videoURL {
print("VIDEO")
let autoIDSto = "media\(media.numMedia).mov"
print(autoIDSto)
let autoID = "media\(media.numMedia)"
let storageRef = Storage.storage().reference().child((Auth.auth().currentUser?.uid)!).child("post:\(postID)").child(autoIDSto)
let postRef = childRef.child("Media")
let uploadData = media.videoURL
let uploadTask = storageRef.putFile(from: vidData, metadata: nil) { (metadata, error) in
print("\(vidData) : Video data")
guard let metadata = metadata else { return }
if let error = error {
print(error)
}
storageRef.downloadURL(completion: { (url, error) in
//a bunch of code to add to DB
if let thumbnailImageData = media.thumbnailImage!.jpegData(compressionQuality: 1.0) {
storageRef.putData(thumbnailImageData, metadata: nil) { (metadata, error) in
storageRef.downloadURL(completion: { (url, error) in
if let thumbnail = url {
mediaRef.updateChildValues(["thumbnail" : "\(thumbnail)"])
Whats wrong and how can I fix it?
You use the same path ( 1 with mov extension )
let storageRef = Storage.storage().reference().child((Auth.auth().currentUser?.uid)!).child("post:\(postID)").child(autoIDSto)
to store the video/image thumbnail here
let uploadTask = storageRef.putFile(from: vidData, metadata: nil) { (metadata, error) in
and
storageRef.putData(thumbnailImageData, metadata: nil) { (metadata, error) in
There should be 2 different paths 1 with mov extension and another with jpg extension that you finally store the reference of both in 1 record in your database table
I am trying to upload an image from ImageView to Firebase storage but it won't work.
I have listed my code below: My image view is called ProfileImage
let storageRef = Storage.storage().reference().child("myImage.png")
if let uploadData = self.ProfileImage.image!.pngData() {
storageRef.putFile(from: uploadData, metadata: nil) { (metadata, error) in
if error != nil {
print("error")
completion(nil)
} else {
// your uploaded photo url.
}
}
}
It comes up with the error "Cannot convert value of type 'Data' to expected argument type 'URL'
You are trying to upload Data, not a file. Replace
putFile
With
putData
And it should work fine
Try this code :-
let storageRef = Storage.storage().reference().child("myImage.png")
if let uploadData = UIImagePNGRepresentation(self.profileImage.image!) {
storageRef.put(uploadData, metadata: nil) { (metadata, error) in
if error != nil {
print("\(error.localizeDescription)")
} else {
// your uploaded photo url.
}
}
let refDatabase = Database.database().reference()
var refstorage = Storage.storage().reference()
let data = image.jpegData(compressionQuality: 1.0) //
let metadata = StorageMetadata()
metadata.contentType = "image/jpeg"
let postkey = refDatabase.child("Post").childByAutoId().key
print(postkey)
let imagename = "PostImage/\(postkey).png"
refstorage = refstorage.child(imagename)
let timestamp = Date().timeIntervalSince1970 // you can use this to track time when it was uploaded.
self.refstorage?.putData(data!, metadata: metadata, completion: { (meta, error) in
if error == nil{
if let imageData = meta?.downloadURL()?.absoluteString{
// DO SOMETHING
} else {
print("Couldn't get the URL for image")
}
}
})
I know that this question has been answered, but there is an easier way to do this. Along with import Firebase and Firebase Storage, you will also have to add FirebaseUI to your podfile and import it.
After you have done that, you could get your image to your app much simpler.
let storage = Storage.storage()
let storageRef = storage.reference()
let placeholderImage = UIImage(named: "placeholder.jpeg")
let reference = storageRef.child("myImage.png")
ProfileImage.sd_setImage(with: reference, placeholderImage: placholderImage)
(The placeholder Image would just be a transparent image that you put in your assets folder in XCode that you could reuse multiple times in your application, for whenever you needed to get a Firebase Image on your app.)
For some reason, when I am using firebase storage to retrieve my download image URL (I have confirmed that the image is actually sent to firebase storage), the url is returning nil. I'm pretty sure my file path matches/is correct but I'm not sure where I'm going wrong on this.
let selectedImageData=UIImageJPEGRepresentation(selectedImage.image!, 1)
let storageRef=Storage.storage().reference()
let metadata=StorageMetadata()
storageRef.child("Users").child(Auth.auth().currentUser!.uid)
.child("Pictures"+String(self.tracker)).putData(selectedImageData!, metadata: metadata)
{
(metadata, error) in
if error != nil{
print("There was a problem uploading")
return
}
storageRef.child("Users").child(Auth.auth().currentUser!.uid)
.child("Pictures"+String(self.tracker)).downloadURL(completion:
{
(url, error) in
print("What's going on")
self.pictures.append((url?.absoluteString)!)
})
}
Use the below code to upload image to Storage and get a return path for the image
let selectedImageData=UIImageJPEGRepresentation(selectedImage.image!, 1)
self.uploadProfileImageToFirebase(data: selectedImageData)
func uploadProfileImageToFirebase(data:NSData){
let storageRef = Storage.storage().reference().child("Users").child(Auth.auth().currentUser!.uid).child("Pictures").child(String(self.tracker))
//Modify the above line as per your requirement
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
let urlStr:String = (url?.absoluteString) ?? ""
print(urlStr)
}
}
})
}
Seems like your reference for putData and for getting downloadURL is different some how, maybe the tracker is changing in the meantime?
To avoid the reference being different let's try changing your code like this:
let selectedImageData=UIImageJPEGRepresentation(selectedImage.image!, 1)
let storageRef=Storage.storage().reference()
let metadata=StorageMetadata()
let reference = storageRef.child("Users").child(Auth.auth().currentUser!.uid).child("Pictures"+String(self.tracker))
reference.putData(selectedImageData!, metadata: metadata) { (metadata, error) in
if error != nil{
print("There was a problem uploading")
return
}
reference.downloadURL { (url, error) in
print("Download error: \(error), url: \(url)")
self.pictures.append((url?.absoluteString)!)
}
}
Basically we make sure to use the same reference in both cases.