I want to save an image from an UIImage as a PFFile. But then I got the error :
Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
As you will see in the snippet I print the data of the image and I got something in it. If I comment the setting of the image to the current user everything is working fine (w/o the image obviously).
Any idea where to look ?
@IBAction func updateProfil(_ sender: Any) {
PFUser.current()!["isFemale"] = genderSwitch.isOn
if let image = profilImage.image {
print(image)
if let data = UIImagePNGRepresentation(image) {
print(data)
PFUser.current()!["image"] = PFFile(name: "profile.png", data: data)
PFUser.current()?.saveInBackground(block: { (s, e) in
if e != nil {
print(e as Any)
self.getErrorFromServer(errServeur: e, errMessage: "Update Failed")
} else {
self.showSuccessMessage(m: "Info uploaded")
print("Data Saved !")
self.performSegue(withIdentifier: "updatedProfile", sender: nil)
}
})
}
}
}
print(image)
and print(data)
give me respectively :
<UIImage: 0x6080000a8580> size {1200, 704} orientation 0 scale 1.000000
1411255 bytes
Usually this happened due to the size of your file. One of parse-server configuration options is maxUploadSize. The default value of it is 20MB means that you will not be able to upload files larger than 20MB. You can override this value in your parse-server configuration to 100MB (unless you are dealing with really big files like: videos etc.)
I think the best solution for you will also be to compress the image before you uploading it to parse-server. Compressing the image will reduce its size dramatically and will keep its quality (because the JPEG compression) so before uploading the image you need to do the following:
let data = UIImageJPEGRepresentation(image, 0.5)
let f = PFFile(data: data, contentType: "image/jpeg")
the 0.5 is the quality, the max value of it is 1 but if you will set it to 0.5 or even 0.4 you will get a very good result. In addition, you can compress the image by scale it down. This is something that will also reduce its size dramatically but its really depends on your use case.
Increasing the size on the parse-server side should be like the following:
var server = ParseServer({
...otherOptions,
// Set the max upload size of files
maxUploadSize: 100MB,
....
});
Good luck with it