Jonah Elbaz Jonah Elbaz - 8 months ago 141
iOS Question

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)
}

Answer Source

You have to set the metadata for the same.

Try it.

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download