使用camfind的Swift Image上传请求

时间:2015-08-07 04:03:24

标签: ios swift api mashape cloudsight

嘿我试图使用Mashape api CamFind上传图片请求,但我不确定该怎么做,Camfind希望: - 多形式编码请求 - 包含密钥的标头 - 参数:image_request [locale]和image_request [image]

我已经坚持了很长时间,感谢任何帮助,我得到400错误

这是我的代码

 func post(){
    var TWITTERFON_FORM_BOUNDARY:String = "AaB03x"
    let url = NSURL(string: "https://camfind.p.mashape.com/image_requests")!
    var request:NSMutableURLRequest = NSMutableURLRequest(URL: url, cachePolicy: NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData, timeoutInterval: 10)
    var MPboundary:String = "--\(TWITTERFON_FORM_BOUNDARY)"
    var endMPboundary:String = "\(MPboundary)--"
    var data:NSData = UIImagePNGRepresentation(imageView.image)
    var filename:String = "image request image"
    var body:NSMutableString = NSMutableString();
    // params

    let path = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! NSString
    let documentDirectoryPath:NSString = path.stringByAppendingString("items.db")

    let nsDocumentDirectory = NSSearchPathDirectory.DocumentDirectory
    let nsUserDomainMask = NSSearchPathDomainMask.UserDomainMask
     let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
        let imageURL = paths[0].stringByAppendingString("image.png")
    UIImagePNGRepresentation(imageView.image).writeToFile(imageURL, atomically: true)





    let parameters:NSDictionary = ["focus[x]": "480", "focus[y]": "640","image_request[language]": "en","image_request[locale]": "en_US","image_request[image]": imageURL]

        for (key, value) in parameters {
            body.appendFormat("\(MPboundary)\r\n")
            body.appendFormat("Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n")
            body.appendFormat("\(value)\r\n")
        }
    let parametersNS : NSData = NSKeyedArchiver.archivedDataWithRootObject(parameters)
    // image upload
    body.appendFormat("%@\r\n",MPboundary)
    body.appendFormat("Content-Disposition: form-data; name=\"\(filename)\"; filename=\"pen111.png\"\r\n")
    body.appendFormat("Content-Type: image/png\r\n\r\n")
    var end:String = "\r\n\(endMPboundary)"
    var myRequestData:NSMutableData = NSMutableData();
    myRequestData.appendData(body.dataUsingEncoding(NSUTF8StringEncoding)!)

    myRequestData.appendData(parametersNS)

    myRequestData.appendData(end.dataUsingEncoding(NSUTF8StringEncoding)!)
    var content:String = "multipart/form-data; boundary=\(TWITTERFON_FORM_BOUNDARY)"
    request.setValue(content, forHTTPHeaderField: "Content-Type")
    request.setValue("\(myRequestData.length)", forHTTPHeaderField: "Content-Length")
    request.HTTPBody = myRequestData
    request.HTTPMethod = "POST"
    request.setValue("9hcyYCUJEsmsh4lNTgpgVX1xRq0Ip1uogovjsn5Mte0ONVBtes", forHTTPHeaderField: "X-Mashape-Key")
    let session = NSURLSession.sharedSession()
    let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in
        if (error != nil) {
            println(error)
        } else {
            let httpResponse = response as? NSHTTPURLResponse
            let statusCode = (response as? NSHTTPURLResponse)?.statusCode ?? -1
            let statusDescripton = httpResponse?.description

            println(httpResponse)


        }
    })

    dataTask.resume()


}

1 个答案:

答案 0 :(得分:1)

我建议直接浏览CloudSight而不是使用Mashape。还有Objective C Cocoapod您可以直接使用或学习。