PFUser.current()?.saveInBackground 错误与 UIImage()
PFUser.current()?.saveInBackground ERROR with UIImage()
我想将 UIImage 中的图像保存为 PFFile。但后来我得到了错误:
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.}
正如您将在代码片段中看到的那样,我打印了图像的数据并且在其中得到了一些东西。
如果我将图像设置评论给当前用户,一切正常(w/o 显然是图像)。
知道去哪里找吗?
@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)
和print(data)
分别给我:
<UIImage: 0x6080000a8580> size {1200, 704} orientation 0 scale 1.000000
1411255 bytes
这通常是由于您的文件大小所致。解析服务器配置选项之一是 maxUploadSize。它的默认值为 20MB,这意味着您将无法上传大于 20MB 的文件。您可以在解析服务器配置中将此值覆盖为 100MB(除非您正在处理非常大的文件,例如:视频等)
我认为对您来说最好的解决方案也是在将图像上传到解析服务器之前压缩图像。压缩图像会显着减小其大小并保持其质量(因为 JPEG 压缩),因此在上传图像之前,您需要执行以下操作:
let data = UIImageJPEGRepresentation(image, 0.5)
let f = PFFile(data: data, contentType: "image/jpeg")
0.5 是质量,它的最大值是 1,但如果将其设置为 0.5 甚至 0.4,您将获得非常好的结果。此外,您可以通过缩小图像来压缩图像。这也会显着减小其大小,但这实际上取决于您的用例。
增加解析服务器端的大小应该如下所示:
var server = ParseServer({
...otherOptions,
// Set the max upload size of files
maxUploadSize: 100MB,
....
});
祝你好运
我想将 UIImage 中的图像保存为 PFFile。但后来我得到了错误:
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.}
正如您将在代码片段中看到的那样,我打印了图像的数据并且在其中得到了一些东西。 如果我将图像设置评论给当前用户,一切正常(w/o 显然是图像)。
知道去哪里找吗?
@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)
和print(data)
分别给我:
<UIImage: 0x6080000a8580> size {1200, 704} orientation 0 scale 1.000000
1411255 bytes
这通常是由于您的文件大小所致。解析服务器配置选项之一是 maxUploadSize。它的默认值为 20MB,这意味着您将无法上传大于 20MB 的文件。您可以在解析服务器配置中将此值覆盖为 100MB(除非您正在处理非常大的文件,例如:视频等)
我认为对您来说最好的解决方案也是在将图像上传到解析服务器之前压缩图像。压缩图像会显着减小其大小并保持其质量(因为 JPEG 压缩),因此在上传图像之前,您需要执行以下操作:
let data = UIImageJPEGRepresentation(image, 0.5)
let f = PFFile(data: data, contentType: "image/jpeg")
0.5 是质量,它的最大值是 1,但如果将其设置为 0.5 甚至 0.4,您将获得非常好的结果。此外,您可以通过缩小图像来压缩图像。这也会显着减小其大小,但这实际上取决于您的用例。
增加解析服务器端的大小应该如下所示:
var server = ParseServer({
...otherOptions,
// Set the max upload size of files
maxUploadSize: 100MB,
....
});
祝你好运