metadata?.downloadURL()?.absoluteString - swift

I have a some problem with old syntax like:
metadata?.downloadURL()?.absoluteString"
How to use new way in my code?
Error in this part code:
let downloadURL = metadata?.storageReference?.downloadURLWithCompletion()
Full code:
Storage.storage().reference().child(imgUid).putData(imgData, metadata: metadata) { (metadata, error) in
if error != nil {
print("Did'n upload image")
} else {
print("uploaded")
let downloadURL = metadata?.storageReference?.downloadURLWithCompletion()
if let url = downloadURL {
self.setUser(img: url)
}
}
}

This is a easyway to do this use this Func to save the data to the FirebaseStorage then take the url as a String then you can save it on
func uploadImageToFirebaseStorage(data: Data, onSuccess: #escaping (_ imageUrl: String) -> Void) {
let photoIdString = NSUUID().uuidString
let storageRef = Storage.storage().reference(forURL: Config.STORAGE_REF_URL).child(POST_REF).child(photoIdString)
storageRef.putData(data, metadata: nil) { (metadata, error) in
if let error = error {
debugPrint(error.localizedDescription)
return
}
metadata?.storageReference?.downloadURL(completion: { (url, error) in
if let error = error {
print(error.localizedDescription)
return
}
onSuccess("\(url!)")
})
}
}

func save() {
let newPostRef = Database.database().reference().child("users").childByAutoId()
let newPostKey = newPostRef.key
// 1. save image
if let imageData = self.profilPic!.jpegData(compressionQuality:0.5) {
let storage = Storage.storage().reference().child("profileImages")
DispatchQueue.main.sync {
storage.putData(imageData).observe(.success, handler: { (snapshot) in
storage.downloadURL(completion: { (url, error) in
if error != nil {
print(error!.localizedDescription)
return
}
if let profileImageUrl = url?.absoluteString {
guard let uid = (Auth.auth().currentUser?.uid) else {return}
let values = ["name": self.name, "email": self.email, "profilePictureUrl": profileImageUrl]
newPostRef.setValue(values)
self.ref.child(uid).childByAutoId().setValue(values)
}
})
})
}
}
}

Related

Completion not working with Firebase Database

The user can upload a profile picture and some information about himself in my app.
I want to write the url of the uploaded picture in firebase realtime database but it takes the placeholder text "testentry" and not the real url. Why does my completion not work here?
var imagePicker: UIImagePickerController!
var urltoPicture = "testentry"
#IBAction func updateProfile(_ sender: UIButton) {
uploadPic(arg: true, completion: { (success) -> Void in
if success {
linkUbertragen()
} else {
}
})
func uploadPic(arg: Bool, completion: #escaping (Bool) -> ()) {
guard let imageSelected = self.image else {
//print("ok")
return
}
guard let imageData = imageSelected.jpegData(compressionQuality: 0.1) else {
return
}
let storageRef = Storage.storage().reference(forURL: "gs://h......com")
let storageProfileRef = storageRef.child("profilePictures").child(Auth.auth().currentUser!.uid)
let metadata = StorageMetadata()
metadata.contentType = "image/jpg"
storageProfileRef.putData(imageData, metadata: metadata, completion: {
(storageMetadata, error) in
if error != nil {
//print(error?.localizedDescription)
return
}
storageProfileRef.downloadURL(completion: { (url, error) in
if let metaImageURL = url?.absoluteString {
print(metaImageURL)
self.urltoPicture = metaImageURL
}
})
})
completion(arg)
}
func linkUbertragen(){
ref = Database.database().reference()
let userID = Auth.auth().currentUser!.uid
ref.child("user/\(userID)").updateChildValues(["profileText": profileText.text!])
print(urltoPicture)
ref.child("user/\(userID)").updateChildValues(["picture": urltoPicture])
}
self.navigationController?.popViewController(animated: true)
}
This is a very common mistake. You have to call completion inside the (final) closure.
And it is good practice to call completion(false) always in case of an error – even better to return and handle all errors
func uploadPic(arg: Bool, completion: #escaping (Bool) -> ()) {
guard let imageSelected = self.image else {
//print("ok")
completion(false); return
}
guard let imageData = imageSelected.jpegData(compressionQuality: 0.1) else {
completion(false); return
}
let storageRef = Storage.storage().reference(forURL: "gs://h......com")
let storageProfileRef = storageRef.child("profilePictures").child(Auth.auth().currentUser!.uid)
let metadata = StorageMetadata()
metadata.contentType = "image/jpg"
storageProfileRef.putData(imageData, metadata: metadata, completion: {
(storageMetadata, error) in
if error != nil {
//print(error?.localizedDescription)
completion(false); return
}
storageProfileRef.downloadURL(completion: { (url, error) in
if let metaImageURL = url?.absoluteString {
print(metaImageURL)
self.urltoPicture = metaImageURL
completion(true)
} else {
completion(false)
}
})
})
}
The arg parameter is actually not needed.

"The file "..." couldn’t be opened because there is no such file" uploading video from phpickerviewcontroller to FirebaseStorageq

I'm trying to upload a video file using phpickerviewcontroller, but I'm running into an issue uploading the URL to FirebaseStorage. Here is some code:
func uploadVideo(videoURL: URL)
{
let storage = Storage.storage()
let storageRef = storage.reference()
let videoRef = storageRef.child("rPosts/\(uid!)/\(fileID)")
let metadata = StorageMetadata()
metadata.contentType = "video/quicktime"
var videoData: Data = Data()
do
{
videoData = try Data(contentsOf: videoURL)
}
catch
{
print(error.localizedDescription)
return
}
videoRef.putData(videoData, metadata: metadata)
{ (metaData, error) in
guard error == nil else
{
self.errorLabel.text = error!.localizedDescription
return
}
}
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult])
{
dismiss(animated: true, completion: nil)
guard let itemProvider = results.first?.itemProvider else { return }
itemProvider.loadItem(forTypeIdentifier: "com.apple.quicktime-movie", options: nil)
{ (videoFile, error) in
guard error == nil else { return }
let videoFile = videoFile as? URL
DispatchQueue.main.async
{
self.uploadVideo(videoURL: videoFile!)
print(videoFile!)
}
self.uploadedYet = true
}
}
I've tried using .putFile but it keeps on saying
Ensure file URL is not a directory, symbolic link, or invalid url.
When I use .putData it says
The file "..." couldn’t be opened because there is no such file
EDIT:
itemProvider.loadFileRepresentation(forTypeIdentifier: "com.apple.quicktime-movie")
{ (videoURL, error) in
guard error == nil else { return }
print("isbeingcalled") //does not get calleed :(
DispatchQueue.main.async
{
let storageRef = Storage.storage().reference()
let videoRef = storageRef.child("rPosts/\(self.uid!)/\(self.fileID).mov")
let metadata = StorageMetadata()
metadata.contentType = "video/quicktime"
print("run")
videoRef.putFile(from: videoURL!, metadata: metadata)
{ (metaData, error) in
guard error == nil else
{
print(videoURL!)
print(videoRef.fullPath)
self.errorLabel.text = error!.localizedDescription
print(error!.localizedDescription)
return
}
}
}
self.uploadedYet = true
}

Why do I have Problem with upload pic in swift. Problem with nil unwrap. URL

** Update**
My point is im trying to match my profilePicLink with image I upload from my library.
Also selectedUsers is Users Type as following
var username : String = ""
var email : String = ""
var uid : String = ""
var profilePicLink : String = ""
init(username : String, email: String, uid : String, profilePicLink: String ) {
self.username = username
self.email = email
self.uid = uid
self.profilePicLink = profilePicLink
}
I am having problem when I am trying to upload photo. The action are
I pick the photo from my library
#IBAction func getPhotoButton(_ sender: Any) {
let image = UIImagePickerController()
image.delegate = self
image.sourceType = UIImagePickerController.SourceType.photoLibrary
self.present(image, animated: true, completion: nil)
}
It leads me to my photo library. After I pick my photo. I click on button "Update" with the action as following code
#IBAction func updatePhoto(_ sender: Any) {
uploadPhoto()
}
func uploadPhoto(){
selectedUser?.uploadProfileImage(imageView.image!){
url in print (URL.self)
}
}
I got the error as ** Fatal error: Unexpectedly found nil while unwrapping an Optional value: ** in the func uploadPhoto as the picture
Fatal Error
And here is the code of func in my other class (Users) for upload and get Profile Image
func getProfileImage() -> UIImage {
if let url = NSURL(string: profilePicLink){
if let data = NSData(contentsOf: url as URL) {
return UIImage(data: data as Data)!
}
}
return UIImage()
}
func uploadProfileImage(_ image:UIImage, completion: #escaping ((_ url:URL?)->())) {
guard let uid = Auth.auth().currentUser?.uid else { return }
let storageRef = Storage.storage().reference().child("user/\(uid)")
guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
storageRef.putData(imageData, metadata: metaData) { metaData, error in
if error == nil, metaData != nil {
storageRef.downloadURL { url, error in
completion(url)
// success!
}
} else {
// failed
completion(nil)
}
}
}
Updated : I modifed my function uploadProfileImage as following. My point is I wanna assign profilePicLink variables to the downloadurl. And then I update value of profilePicLink
func uploadProfileImage(_ image:UIImage, completion: #escaping ((_ url:URL?)->())) {
let storageRef = Storage.storage().reference().child("profileImages").child("\(NSUUID().uuidString).jpg")
guard let imageData = image.jpegData(compressionQuality: 0.75) else { return }
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
storageRef.putData(imageData, metadata:metaData) { (metaData, error) in
if error != nil, metaData != nil {
storageRef.downloadURL (completion: {(url, error) in
if error != nil {
if let downloadurl = url?.absoluteString {
if (self.profilePicLink == "") {
self.profilePicLink = downloadurl
Database.database().reference().child("users").child(self.uid).updateChildValues(["profilePicLink":downloadurl])
}
}
} else {
completion(nil)
}
}
)
}
}
}
Please be advised on this.

Wait for image to be uploaded before continuing function - Swift 5 Firebase Storage

I'm trying to upload an image using Firebase Storage. The problem i'm having is when i'm calling my image upload function, the upload task is very slow and it ends up running last in my main function when it is actually called further up.
Is there a way to wait for storageRef.putData to finish before continuing the rest of the function?
Would possibly reducing compression quality from 1 be faster?
Here is my function:
func newImageUrl(){
let key = self.itemId
guard let userID = Auth.auth().currentUser?.uid else { return }
let storageRef = Storage.storage().reference().child(userID).child("SubCategory").child(self.itemTitle!).child(key!).child("ItemImages.jpg")
guard let imageData = self.itemImage.image!.jpegData(compressionQuality: 1) else { return }
print("starting image upload!")
storageRef.putData(imageData, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
return
}
storageRef.downloadURL { (url, error) in
guard let urlStr = url else{
return
}
let urlFinal = (urlStr.absoluteString)
self.imageFinalUrl = urlFinal
}
}
}
UPDATED func --
func newImageUrl(completion:#escaping((String?) -> () )) {
let key = self.itemId
guard let userID = Auth.auth().currentUser?.uid else { completion(nil) ; return }
let storageRef = Storage.storage().reference().child(userID).child("SubCategory").child(self.itemTitle!).child(key!).child("ItemImages.jpg")
guard let imageData = self.itemImage.image!.jpegData(compressionQuality: 1) else { completion(nil) ; return }
print("starting image upload!")
storageRef.putData(imageData, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
return
}
storageRef.downloadURL { (url, error) in
guard let urlStr = url else{
completion(nil)
return
}
let urlFinal = (urlStr.absoluteString)
self.imageFinalUrl = urlFinal
completion(urlFinal)
}
}
}
Example main function:
func updateItemDetail(){
print("this is running 1")
self.newImageUrl { (str) in
print(str)
}
print("this is running 2")
Console prints:
this is running 1
starting image upload!
this is running 2
Optional("https://firebasestorage.googleapis.com/v0/b/.....etc")
You need a completion
func newImageUrl(completion:#escaping((String?) -> () )) {
let key = self.itemId
guard let userID = Auth.auth().currentUser?.uid else { completion(nil) ; return }
let storageRef = Storage.storage().reference().child(userID).child("SubCategory").child(self.itemTitle!).child(key!).child("ItemImages.jpg")
guard let imageData = self.itemImage.image!.jpegData(compressionQuality: 1) else { completion(nil) ; return }
print("starting image upload!")
storageRef.putData(imageData, metadata: nil) { (metadata, error) in
guard let metadata = metadata else {
return
}
storageRef.downloadURL { (url, error) in
guard let urlStr = url else{
completion(nil)
return
}
let urlFinal = (urlStr.absoluteString)
self.imageFinalUrl = urlFinal
completion(urlFinal)
}
}
}
Call
newImageUrl { (str) in
print(str)
// do your next work here
}

downloadURL from Firebase Storage is nil after successfully uploading a file

I'm new to firebase and I'm facing a problem here.
I have successfully uploaded my image to firebase storage already, however, I need to get the downloadurl at once and post it to the database. And I'm not sure why it doesn't work within that closure.
The following is my code:-
func CreateNewChatRoom(user: User, caption: String, data: Data){
let filePath = "\(user.uid)/\(Int(NSDate.timeIntervalSinceReferenceDate)).jpg"
let metaData = StorageMetadata()
metaData.contentType = "image/jpg"
let reference = storageRef.child(filePath)
reference.putData(data, metadata: metaData) { (metadata, error) in
if let error = error {
print("\(error.localizedDescription)")
return
}
reference.downloadURL(completion: { (url, error) in
if let error = error{
print("Error : \(error.localizedDescription)")
return
}
else{
if let downloadURL = url?.absoluteString {
let idRoom = self.BASE_REF.child("rooms").childByAutoId()
idRoom.setValue(["caption":caption, "thumbnailUrlFromStorage": self.storageRef.child(metadata!.path!).description,"fileUrl": downloadURL])
}
}
})
}
}
I have the same problem, and I found the following solution,
I hope it is useful to someone.
Swift 5
import FirebaseAuth
import FirebaseStorage
import FirebaseFirestore
typealias AddStoryResult = (Bool, Error?) -> Void
typealias UploadImageResult = (StorageMetadata?, Error?) -> Void
class StoryFirebaseAPI {
static let storage = Storage.storage()
static var downloadUrlString: URL?
var image: UIImage?
let photoUri = "smth uri path"
uploadImage(image, photoUriString: photoUri) { (metadata: StorageMetadata?, error: Error?) in
guard let metadata = metadata else {
if let error = error {
print("Error occured: \(error)")
}
completion(false, error)
return
}
// and at this point I have print(downloadUrlString)
completion(true, nil)
}
}
extension StoryFirebaseAPI {
func uploadImage(_ image: UIImage, photoUriString: String, completion: #escaping UploadImageResult) {
guard let imageData = image.jpegData(compressionQuality: 0.5) else {
print("smth error")
completion(nil, nil)
return
}
let storageRef = storage.reference().child(photoUriString)
let imageMetadata = StorageMetadata()
imageMetadata.contentType = "image/jpeg"
storageRef.putData(imageData, metadata: imageMetadata) { (storageMetadata, error) in
if let error = error {
print("Error: \(error.localizedDescription)")
completion(nil, error)
return
}
guard let unwrappedMetadata = storageMetadata else {
print("smth error")
print("Error: \(error?.localizedDescription ?? "")")
completion(nil, error)
return
}
storageRef.downloadURL { url, error in
if let error = error {
print("Error: \(error.localizedDescription)")
completion(nil, error)
return
} else {
if let url = url {
downloadUrlString = url
completion(unwrappedMetadata, nil)
}
}
}
print("Image uploaded successfully")
}
}
}