I have looked around for an answer to this. The closest I got is here, however, It does not exactly answer my question. That being, how to store a reference to images which
Please modify your code as required
var imgData: NSData = NSData(data: UIImageJPEGRepresentation((self.img_Photo?.image)!, 0.8)!)
self.uploadProfileImageToFirebase(data: imgData)
func uploadProfileImageToFirebase(data:NSData){
let storageRef = Storage.storage().reference().child("usersPosts").child("\(uid).jpg")
if data != nil {
storageRef.putData(data as Data, metadata: nil, completion: { (metadata, error) in
if(error != nil){
print(error)
return
}
guard let userID = Auth.auth().currentUser?.uid else {
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) ?? ""
let values = ["downloadURL": urlStr]
self.addImageURLToDatabase(uid: userID, values: values as [String : AnyObject])
}
}
})
}
}
func addImageURLToDatabase(uid:String, values:[String:AnyObject]){
let ref = Database.database().reference(fromURL: "https://exampleapp.firebaseio.com/")
let usersReference = ref.child("usersPosts").child((Auth.auth().currentUser?.uid)!)
usersReference.updateChildValues(values) { (error, ref) in
if(error != nil){
print(error)
return
}
self.parentVC?.dismiss(animated: true, completion: nil)
}
}
(TS)
onFileChanged(param){
var aa;
const file: File = param.target.files[0];
const metaData = { 'contentType': file.type };
const StorageRef: firebase.storage.Reference = firebase.storage().ref('/photos/Category/'+this.CategoryName);
const Store = StorageRef.put(file, metaData);
setTimeout(() => {
const UP: firebase.storage.UploadTask = Store;
UP.snapshot.ref.getDownloadURL().then(function (downloadURL) {
console.log('File available at', downloadURL);
aa = downloadURL;
});
}, 1000);
setTimeout(() => {
this.ImageLink = aa;
debugger;
}, 2000);
IN HTML
type="file" accept="image/*" #file style="display: none">
<img (click)="file.click()" style="margin-left: 10%"src="http://icons.iconarchive.com/icons/icons8/windows-8/512/Photo-Video-Stack-Of-Photos-icon.png" width="50px" />
import * as firebase as '@ionic/firebase'