Encoding UIImage to Base64 string not working when transferred to server

孤者浪人 提交于 2019-12-08 11:08:30

问题


Encoding an UIImage as a Base64 string works on the device, but transferring the string to the server somehow corrupts the string and prevents the server from successfully decoding the image.

Any suggestions on the problem?

        // Define params
        params["thumbnail_base64"] = imageToBase64(blockSet.thumbnailURL)
        ...

        // Convert params -> query string
        let postString = buildQueryString(params)

        // Define upload URL
        let uploadURL = NSURL(string: RootURL + UploadFilePath)!

        // Hit server
        let request = NSMutableURLRequest(URL: uploadURL)
        request.HTTPMethod = "POST"
        request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
        ...

private func buildQueryString(parameters: [String:String], omitQuestionMark: Bool = false) -> String {
    var urlVars = [String]()
    for (k, var v) in parameters {
        v = v.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())!
        urlVars += [k + "=" + "\(v)"]
    }
    return ((urlVars.isEmpty || omitQuestionMark) ? "" : "?") + urlVars.joinWithSeparator("&")
}


private func imageToBase64(filename: String) -> String {
    // Get image path
    let imagePath = getFilePath(filename)

    // Convert image to base64 or return empty string
    if let imageData = NSData(contentsOfFile: imagePath) {
        let base64String = imageData.base64EncodedStringWithOptions(.EncodingEndLineWithLineFeed)
        return base64String
    } else {
        printError("Error converting image to Base64: missing image. Filename: \(filename)")
        return ""
    }
}

回答1:


The problem is with the queryString, base64 is long text with many characters, let JSON do the work for you

Use the next (with some example of NodeJS)

  let params = NSMutableDictionary();
  //you can only set `serializable` values
  params.setValue(imageToBase64(),forKey:"base64")
  params.setValue(username,forKey:"username")
  params.setValue(["array","of","string"],forKey:"arr")

  let uploadURL = NSURL(string: theURL)!

  // Hit server
  let request = NSMutableURLRequest(URL: uploadURL)
  request.HTTPMethod = "POST"

  request.setValue("application/json", forHTTPHeaderField: "Content-Type")

  do {
      let jsonData = try NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions(rawValue: 0))
      request.HTTPBody = jsonData
      let session = NSURLSession.sharedSession()

      session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
          print("Response: \(response)")
      }).resume()


  } catch let error as NSError {
      print(error)
  }

nodejs:

var buffer = new Buffer(request.body["base64"], 'base64')
fs.writeFile('test.jpeg',buffer,"base64"); //Works
var username = request.body["username"];
var someStringsArr  = request.body["arr"]

by the way...

you wrote the function buildQueryString, which is already exists in Foundation

let urlComponents = NSURLComponents(string: "http://myUrl.com/getApi/")!
urlComponents.queryItems = [NSURLQueryItem]()
urlComponents.queryItems!.append(NSURLQueryItem(name:"myKeyA",value:"myValueA"))
urlComponents.queryItems!.append(NSURLQueryItem(name:"myKeyB",value:"myValueB"))

print(urlComponents.URL!) //http://myUrl.com/getApi/?myKeyA=myValueA&myKeyB=myValueB

Use url query if want to send GET parameters via the URL



来源:https://stackoverflow.com/questions/37034221/encoding-uiimage-to-base64-string-not-working-when-transferred-to-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!