Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/115.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-iOS中的旋度等价_Ios_Swift_Curl - Fatal编程技术网

Swift-iOS中的旋度等价

Swift-iOS中的旋度等价,ios,swift,curl,Ios,Swift,Curl,我尝试了不同的方法来创建这个cURL请求的swift等价物,但是我无法让它工作 curl -X POST -F "file=@/Users/nicolas/sample.png" -F "mode=document_photo" https://api.idolondemand.com/1/api/sync/ocrdocument/v1 -F "apikey=xxx-xxx-xxx-xxx-xxx" 相关代码如下所示 func getText (image: UIImage){ le

我尝试了不同的方法来创建这个cURL请求的swift等价物,但是我无法让它工作

curl -X POST -F "file=@/Users/nicolas/sample.png" -F "mode=document_photo" https://api.idolondemand.com/1/api/sync/ocrdocument/v1 -F "apikey=xxx-xxx-xxx-xxx-xxx"
相关代码如下所示

func getText (image: UIImage){

    let apiKey = "xxx-xxx-xxx-xxx-xxx"

    let request = NSMutableURLRequest(URL: NSURL(string: "https://api.idolondemand.com/1/api/sync/ocrdocument/v1")!)
    request.HTTPMethod = "POST"
    request.addValue(apiKey, forHTTPHeaderField: "apikey")
    request.addValue("document_photo", forHTTPHeaderField: "mode")
    request.HTTPBody = UIImageJPEGRepresentation(image, 1)

    let task = NSURLSession.sharedSession().uploadTaskWithRequest(request, fromData: UIImageJPEGRepresentation(image, 1), completionHandler: {data, response, error -> Void in


        if let _ = data {
            var error:NSError? = nil
            do {
                let jsonObject : AnyObject = try NSJSONSerialization.JSONObjectWithData(data!, options: [])
                let json = JSON(jsonObject)
                if let ocr_results = json["text_block"][0]["text"].string {
                    self.returnText(ocr_results)
                }
            } catch let error1 as NSError {
                error = error1
                print(error)
            } catch {
                fatalError()
            }
        }

})

如果得到响应,我会很高兴。

在curl命令中,您正在定义表单字段
文件
模式
apikey
,您需要将它们编码为表单
多部分/表单数据
,并将其放入
HTTPBody
。一些快速的谷歌搜索会发现这个库(以及许多其他库),这将帮助您提出此类请求。

您搜索了什么来找到这个库?因为这个库在swift 2.0中不起作用,所以我可以搜索另一个使用swift 2.0的库。我搜索了“多部分/表单数据”。请注意,您还可以使用Objective-C库,其中有许多。大多数HTTP请求库都能做到这一点——如果它们说它们支持文件上传/发送,这就是实现这一点的方式。而且,大多数库会完全为您设置
NSURLRequest
(因此,例如,您不会获得必须分配给
HTTPBody
)的
NSData
).我试着用Alamofire来做,但我无法让它工作,因为这是我第一次使用api。你知道如何使用阿拉莫菲尔吗?我以前从未听说过Alamofire,但在它的Github页面上,我发现自述文件中有一个有趣的部分叫做。与文件一样,
mode
apikey
字段需要添加appendBodyPart,而且它看起来需要NSData,因此需要将字符串编码为UTF-8或其他编码。但我会让阿拉莫菲尔的专家来解决这个问题。