I want to upload an image using its string representation to conform to my network function. code of the function (this one is working well)
func uploadItemPict(itemdata:String, itempicture:String, completion: @escaping (AsyncResult<Any>)->())
{
let myUrl = URL(string: urlAPIServer);
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
var postString : String
postString = "id="
postString += itemdata
postString += "&ip="
postString += itempicture
postString += "&cmd=uploadItemPicture"
request.httpBody = postString.data(using: String.Encoding.utf8)
let session = URLSession.shared
let task = session.dataTask(with: request as URLRequest){
(data, response, error) -> Void in
if let error = error
{
completion(AsyncResult.Failure(error as NSError?))
} else {
//let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
//print("*********response data = \(String(describing: responseString))")
//completion(AsyncResult.Success(responseString as Any))
if let data = data {
var json = try? JSONSerialization.jsonObject(with: data, options: [])
if(json != nil) {
print(json!)
completion(AsyncResult.Success(json as Any))
}
else {
json = "{\"cmd\":\"uploadItemPict\",\"success\":\"false\",\"message\":\"wrong result: check if debug on on ws\"}"
print(json!)
let myerror = NSError(domain: "com.error.ws", code: 0, userInfo: [NSLocalizedDescriptionKey : "Wrong data from website"])
completion(AsyncResult.Failure(myerror as NSError?))
}
}
}
}
task.resume()
}
here is the code to get the picture string:
@IBAction func sendToto() {
guard let url = Bundle.main.path(forResource: "toto", ofType: "jpg")
else {
// File Error
print ("File reading error")
return
}
let imageIn64String = UIImageJPEGRepresentation(UIImage(contentsOfFile: url)!, 0.7)?.base64EncodedString(options: .endLineWithLineFeed)
if(isInternetAvailable()) {
NSLog("internet available")
uploadItemPict(itemdata: imageIn64String!, itempicture: "titi.jpg") {
(result) in
switch result
{
case .Success(let result):
print("->success")
break
case .Failure(let error):
print("->not a success")
break
}
}
}
}
I effectively get a string on the php side. I got an error with the method imagecreatefromstring so I decided to upload the toto.jpg on the server side and to get the local string, base64encoded to compare the 2 strings:
$itemData = $this->getStringImage('id');
$itempicture = $this->getString('ip', 255);
$itemDataLocal = file_get_contents("images/toto.jpg");
$itemDataLocal = base64_encode($itemDataLocal);
print($itemData);
print("<br>**************************************************<br>");
//print($itemDataGot);
//print("<br>**************************************************<br>");
print($itemDataLocal);
$itemDataLocal = base64_decode($itemDataLocal);
$image = imagecreatefromstring($itemDataLocal);
imagejpeg($image, "images/".$itempicture, 0.7);
When I compare the received string ($itemData) with the local ($itemDataLocal - with which, encoded and then decoded, I succeed in recreating the image), I got different string, and the received string from IOS is not recognized as a correct DATA string to create an image.
Any Idea where I could do something wrong?
Thanks
EDIT: I though it was an issue of encoding in base64 in IOS, as the result between iOS and PHP is different.
As I had a doubt with the UIImageJPEGRepresentation, I thus tried to use something different: toto.jpg is the exact same file on iOS and on the server. On iOS, I tried the following:
//Use image's path to create NSData
let url:NSURL = NSURL(string : "http://SERVER/app/images/toto.jpg")!
//Now use image to create into NSData format
let imageData:NSData = NSData.init(contentsOf: url as URL)!
let imageIn64String = imageData.base64EncodedString()
print(imageIn64String)
in this case, i have the same base64 string result as the one i had with php. I thus suspect the issue might come from: UIImageJPEGRepresentation.
Any Idea? Thanks again
EDIT2: I got this working:
guard let url1 = Bundle.main.path(forResource: "toto", ofType: "jpg")
else {
// File Error
print ("File reading error")
return
}
let imageData:NSData = NSData.init(contentsOfFile:url1)!
let imageIn64String = imageData.base64EncodedString()
for the base64 encode string. This string contents some "+" symbols. When I get the string on the php side, the "+" disappeared, and thus the image can't be recreated. I'm using this to get the string:
$string = $_POST[$name];
I checked the httprequest to make sure I send the string including the "+"s:
request.httpBody = postString.data(using: String.Encoding.utf8)
NSLog(String(data: request.httpBody!, encoding: String.Encoding.utf8)!)
I can see the "+"s in the request.
What am I doing wrong?