Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save image to firebase as image/jpeg using swift

I'm trying to upload pictures to firebase with a mime type of image/jpeg but for some reason they all upload as application/octet-stream.

Heres my code snippet:

let storageRef = Storage.storage().reference().child("ProfilePictures/\(image).jpg")
        if let uploadData = UIImageJPEGRepresentation(self.pickedImage!, 0.5){
            storageRef.putData(uploadData, metadata: nil, completion:{ (metadata, error) in })

I got the image through a picker from the photo gallery

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {

        ImageButton.setBackgroundImage(editedImage, for: .normal)
        ImageButton.setTitle("", for: .normal)
        self.pickedImage = editedImage
    }

    dismiss(animated: true, completion: nil)
}
like image 734
Jonah Elbaz Avatar asked Dec 23 '17 00:12

Jonah Elbaz


People also ask

How do I convert a bitmap image to Base64 in Firebase?

mImageLabel.setImageBitmap(imageBitmap); sets our detail view's ImageView to contain the imageBitmap object returned from the camera. This immediately places the new photo in the detail view. We then call a custom method that will encode our image in Base64 and save it to Firebase.

How do I save a JPEG image from uiimage?

Saving a jpeg image is just as easy as saving a png. UIImage has a built in method called jpegData which will allow us to convert our UIImage to jpeg data. To save a jpeg file we will use the following code:

Why do we need to de-code images in Firebase?

Now that our images are encoded and saved in Firebase, we need to be able to de-code them to retrieve them and display them back into our application.

How to handle errors during uploading files to cloud storage for Firebase?

After a successful upload, we can fetch the download URL of the uploaded image and then fetch it with any image loader, e.g. Kingfisher. There could be errors during uploading files to Cloud Storage for Firebase at both server and client sides. We can handle the errors with the observe function.


1 Answers

You have to set the metadata for the same.

Try it.

metadata = { contentType: 'image/jpeg'};

like image 69
J.K.Rai Avatar answered Oct 03 '22 14:10

J.K.Rai