Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift Firebase iOS图像函数返回';JSON中位置1'处的意外标记o;_Swift_Firebase_Firebase Authentication_Google Cloud Functions_Google Vision - Fatal编程技术网

Swift Firebase iOS图像函数返回';JSON中位置1'处的意外标记o;

Swift Firebase iOS图像函数返回';JSON中位置1'处的意外标记o;,swift,firebase,firebase-authentication,google-cloud-functions,google-vision,Swift,Firebase,Firebase Authentication,Google Cloud Functions,Google Vision,我正在使用Xcode 12.4中的Firebase Cloud Functions API和Swift编写一个图像识别器,如下所示: 导入Firebase 导入UIKit 进口基金会 类图像识别器{ 让imageName:字符串 lazy var functions=functions.functions() init(imageName:String){ self.imageName=imageName } 函数识别(){ 打印(“识别”) 如果让userImage=UIImage(名称:im

我正在使用Xcode 12.4中的Firebase Cloud Functions API和Swift编写一个图像识别器,如下所示:

导入Firebase
导入UIKit
进口基金会
类图像识别器{
让imageName:字符串
lazy var functions=functions.functions()
init(imageName:String){
self.imageName=imageName
}
函数识别(){
打印(“识别”)
如果让userImage=UIImage(名称:imageName){
打印(“图像有效”)
guard let imageData=userImage.jpegData(压缩质量:1.0)else{return}
打印(“图像数据有效”)
让base64encodedImage=imageData.base64EncodedString()
让requestData=[
“图像”:[“内容”:base64encodedImage],
“功能”:[“类型”:“文本检测”],
“imageContext”:[“LanguageHights”:[“sa”]]
]
functions.httpscalable(“annotateImage”).call(requestData){(result,error)在
如果let error=错误为N错误{
如果error.domain==functionErrorDomain{
let code=functionErrorCode(原始值:error.code)
let message=error.localizedDescription
let details=error.userInfo[functionErrorDetailsKey]
打印(“错误\(消息)、代码\(代码)、详细信息\(详细信息)”)
}
打印(“结果\(结果)”)
}
guard let annotation=(结果?.data为?[String:Any])?[“fullTextAnnotation”]为?[String:Any]else{return}
打印(“%n完整注释:”)
将text=注释[“text”]设为?字符串??“
打印(“%n\(文本)”)
}
}
}
}
index.js中我的云函数如下:

“严格使用”;
defineProperty(导出,“\uu esModule”{value:true});
exports.annotateImage=void 0;
常量函数=需要(“firebase函数”);
const vision_1=require(“@google cloud/vision”);
const client=new vision_1.default.ImageAnnotatorClient();
//这将只允许具有身份验证令牌的请求访问Vision
//API,包括匿名API。
//强烈建议仅限制登录用户的访问权限。今年五月
//可通过向if语句添加以下条件来完成:
//| | context.auth.token?.firebase?.sign_in_provider===“匿名”
//
//对于更细粒度的控制,您可以添加其他故障检查,即:
//| | context.auth.token?.firebase?.email_verified==false
//另见:https://firebase.google.com/docs/auth/admin/custom-claims
exports.annotateImage=functions.https.onCall(异步(数据、上下文)=>{
console.log(“数据:”+数据);
如果(!context.auth){
抛出新函数.https.HttpsError(“未经身份验证”,“身份验证时必须调用annotateImage”);
}
试一试{
返回wait client.annotateImage(JSON.parse(data));
}
捕获(e){
抛出新函数.https.HttpsError(“内部”,e.message,e.details);
}
});
JSON.parse(数据)部分不起作用-它返回错误:

认识到 图像有效 图像数据有效 2021-03-13 07:57:37.915895+0530图像阅读器[10575:10270760][]nw协议获取图像块调用dlopen libquic失败 错误:JSON中位置1处出现意外标记o,代码可选(_C.FIRFunctionsErrorCode),详细信息为零 结果为零

即使当我将任何其他字典更改为requestData时,JSON仍然无法通过。有人知道如何从iOS正确调用Firebase云函数吗?

这些“功能”:[“类型”:“文本检测”]需要是一系列功能,而Swift不喜欢:

let requestData=[
“图像”:[“内容”:用户图像],
“功能”:[[“类型”:“文本检测”]],
“imageContext”:[“LanguageHights”:[“sa”]]
]
有效的新代码(未完成重构):

导入Firebase
导入UIKit
进口基金会
类图像识别器:可编码{
让imageName:字符串
lazy var functions=functions.functions()
init(imageName:String){
self.imageName=imageName
}
func(){
结构数据:可编码{
let image:[字符串:数据]
let features=[[“type”:“TEXT_DETECTION”]]
让imageContext=[“LanguageHights”:[“sa”]]
init(){
让userImage=UIImage(名为:“onlytext.jpg”)!
让imageData=userImage.jpegData(压缩质量:1.0)!
图像=[“内容”:图像数据]
}
}
let encoder=JSONEncoder()
让encodedData=try!encoder.encode(data())
让string=string(数据:encodedData,编码:.utf8)!
functions.httpscalable(“annotateImage”).call(string){(result,error)in
如果let error=错误为N错误{
如果error.domain==functionErrorDomain{
let code=functionErrorCode(原始值:error.code)
let message=error.localizedDescription
let details=error.userInfo[functionErrorDetailsKey]
打印(“错误\(消息)、代码\(代码)、详细信息\(详细信息)”)
}
}
打印(“成功”)
打印(“结果\(结果?.data)”)
guard let annotation=(结果?.data为?[String:Any])?[“fullTextAnnotation”]为?[String:Any]else{return}
打印(“%n完整注释:”)
将text=注释[“text”]设为?字符串??“