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)
}
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.
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:
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.
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.
You have to set the metadata for the same.
Try it.
metadata = { contentType: 'image/jpeg'};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With