我正在将UIImages转换为base64EncodedString,如下所示:
let imageData:Data = UIImagePNGRepresentation(image!)!
let imageStr = imageData.base64EncodedString()
我正试图将其传递给ASP.NET API Controller,如下所示:
func postLandGradingImages(_ cellHolder: Array<ImagesData>, completion: @escaping (_ result: String) -> Void)
{
//Define Array of Dictionary
var jsonArrayOfDictionaries = [[AnyHashable: Any]]()
//For each item in the cellHolder
for i in 0..<cellHolder.count {
//Define Dictionary for grading data
var jsonDict = [AnyHashable: Any]()
//Set Lot
jsonDict["jobNo"] = cellHolder[i].jobNo
//Set Image
jsonDict["imageBytes"] = cellHolder[i].ImageBytes
//Add Dictionary to Array of Dictionary
jsonArrayOfDictionaries.append(jsonDict)
//Clear Dictionary
jsonDict = [AnyHashable: Any]()
}
//Convert Array of Dictionary to JSON Data
let jsonData: Data? = try? JSONSerialization.data(withJSONObject: jsonArrayOfDictionaries, options: .prettyPrinted)
//Define our URL with the credentials
let urlComponents = NSURLComponents(string: webservice + "PostGradingImages");
//Assign Username
urlComponents?.user = appDelegate.username;
//Assign Password
urlComponents?.password = appDelegate.password;
//Define our URL String
let url = urlComponents?.url;
//Define URL Request
var request = URLRequest(url: url!)
//Set Header Values for request
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
//Set Header Values for request
request.setValue("application/json", forHTTPHeaderField: "Accept")
//Set Request Method to POST
request.httpMethod = "POST"
//Set Request Body to JSON Data
request.httpBody = jsonData
let returnedData = String()
//Start out URL Session to post data
URLSession.shared.dataTask(with: request, completionHandler: {
(data, response, error) in
//If there is an error return false.
if(error != nil){
//return false
completion(returnedData)
}else{
//Get the response
let responseString = String(data: data!, encoding: .utf8)
OperationQueue.main.addOperation({
//return true
completion(responseString!)
})
}
}).resume()
}
问题是,我的.NET代码不喜欢我的base64EncodedString,我从.NET得到这个错误:
对象引用未设置为对象的实例。
我想要完成的是将base64EncodedString从UIImage发送到.NET,这样我就可以将其转换为字节,这样我就可以保存它。但我的.NET不接受我的base64EncodedString ....我做错了什么?