I'm trying to send image to server via JSON string. Problem is the server didn't see my image in PNG or JPG format. Here is code how i do it:
enter image description here
That how i convert parameters to JSON string
enter image description here
What i want - it's encode UIImage to base64 string and send to server.
Thank You!
try using the below code and check if any error occurs or image value is nil
if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
if let imageData = UIImageJPEGRepresentation(image, 0.5) {
let base64String = imageData.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
let dict: [String: Any] = ["data": base64String]
do {
let data = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)
if let string = String(data: data, encoding: .utf8) {
socket.write(string)
}
} catch {
print(error.localizedDescription)
}
}
}
Related
I am trying to convert an image into data and then that data into a string using this code but it fail on conversion when i want to convert data into string
let data = UIImagePNGRepresentation(selectedImageImageView.image!)
let datastring = String(data: data!, encoding: .utf8)
it gives some in data, but always nil in datastring
cant figure it out where the problem is if anyone knows please help... thanks
if let img = selectedImageImageView.image {
if let data = UIImagePNGRepresentation(img) {
if let datastring = data.base64EncodedStringWithOptions(.Encoding64CharacterLineLength) {
println(datastring)
}
}
}
I am trying to upload image file to my server. Below screen shows call through postman.
With KEY : "image" and Value : 123.png [image file] in form-data Body.
I want to implement the same in my app using swift. I tried different solution but dint find a proper solution.
I am a selecting image with help of UIImagePicker:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: nil)
uploadImage(image: image!, url: "profile_image/")
}
I call uploadImage function to upload image.
func uploadImage(image: UIImage, url: String) {
let urlString = "http://example.com/path/"+url
guard let url = URL(string: urlString) else { return }
let request = NSMutableURLRequest(url: url)
request.httpMethod = "POST"
let token = UserDefaults.standard.object(forKey: "token") as? String
request.addValue("Token "+token!, forHTTPHeaderField: "Authorization")
let imageData = UIImagePNGRepresentation(image)!.base64EncodedData()
//let postString = "image=\(imageData))"
//request.httpBody = postString.data(using: .utf8)
let imgDict = ["image": imageData]
do {
let jsonBody = try JSONEncoder().encode(imgDict)
print(jsonBody)
request.httpBody = jsonBody
} catch let jsonError {
print(" Parsing Error: "+jsonError.localizedDescription)
}
URLSession.shared.dataTask(with: request as URLRequest) { (data, response, error) in
if error != nil {
print("Image upload API Error: "+error!.localizedDescription)
}
guard let data = data else { return }
do {
print(data)
let responseData = try JSONDecoder().decode(BasicResponseParameter.self, from: data)
print(responseData)
if responseData.success {
print("Image uploaded")
}
else {
print("Image upload API Failed : "+responseData.message!)
DispatchQueue.main.async(execute: {
popAlert(title: "FAILED", message: responseData.message!, owner:self)
})
}
} catch let jsonError {
print("Image upload API JSON Error :"+jsonError.localizedDescription)
}
}.resume()
}
Any help appreciated.
I would suggest that you encode (when sending the image to your server) and decode (when querying the image back) your image into base64 format
To encode
let img = /// this is your image
let encodedString = UIImagePNGRepresentation(img)?.base64EncodedString()
To Decode:
let data_from_response = /// from your response
let decodeData = Data(base64Encoded: data_from)
Since you are sending a dictionary over your request body, you will need to convert your dictionary to a json
guard let imgDataString = String.init(data: encodedString, encoding: String.Encoding.utf8) else { return }
let imgDict = ["image": imgDataString ]
let jsonData = try! JSONSerialization.data(withJSONObject: imgDict, options: .prettyPrinted)
request.httpBody = jsonData
Side Note Avoid implicit optional unwrapping meaning !, either use if let or guard statements
This is how I create Data from UIImage:
let data = UIImagePNGRepresentation(image)
And then I need to convert it to String;
if let data = data {
let stringFromData = String(data: data, encoding: .utf8)
}
but stringFromData is nil. Why?
You can get it using the Data method base64EncodedString()
if let data = data {
let stringFromData = data.base64EncodedString()
// to decode base 64 string you can use Data base64Encoded String initializer
if let dataFromBase64 = Data(base64Encoded: stringFromData) {
print(data)
}
}
Convert Your image data in Base64 string
For Encode
let stringFromData : Data = Data(base64Encoded: strBase64, options: .ignoreUnknownCharacters)!
And decode
let strBase64 = imageData.base64EncodedStringWithOptions(.allZeros)
When I get my JSON back from my API this is how it looks
{
data:[
100,
80,
105,
99,
etc
]
}
How do I take this array and turn it back into a base64 string, then NSData and finally UIImage.
Here is what I have thus far.
let byteArray = todo["image"]["data"].arrayObject
var data = NSData(bytes: byteArray!, length: byteArray!.count)
var image = UIImage(data: data)
When printing the data it prints fine but returns nil for image.
Have you tried iterating throug the array and building a string from its elements, the use base64 encoding/decoding api to get back from string to NSData? Something like that(I'm writing from iPad so I can't check).
var encodedString=""
for smallString in byteArray {
encodedString += String(smallString)
}
let data = NSData(base64EncodedString: base64Encoded, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
var image = UIImage(data: data)
let byteArray = todo["image"]["data"].arrayObject
let string = String(bytes: byteArray, encoding: .utf8)
let encodedImageData = string
let imageData = NSData(base64Encoded: encodedImageData!)
let image = UIImage(data: imageData! as Data)
I'm kinda new to swift and i need some help with encoding some image, putting it in a JSON and after retrieving it, decoding it back to NSData and recreating the image in an UIImage view controller.
I've found this post Convert Image to Base64 string in iOS + Swift but i get stuck with this part:
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.fromRaw(0)!)
because the fromRaw method is not available anymore.
Thanks in advance
Later edit:
I'm using swiftyJson to parse the array and i'm getting the image data like this:
var base64String = arrayJson[0]["photo"].stringValue
var imageString = base64String as NSString
and after that i'm trying to decode it like this:
let decodedData = NSData(base64EncodedString: imageString, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)
I've also tried with the rawValue instead of IgnoreUnknownCharacters. Both return nil. Also tried with the base64String instead of imageString. Same thing.
You can do the following instead to make a base64 encoded string to an UIImage:
//base64 string to NSData
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0))
//NSData to UIImage
var decodedIamge = UIImage(data: decodedData!)
NSDataBase64EncodingOptions.fromRaw(0)! now is changed to NSDataBase64DecodingOptions(rawValue: 0)
For more encode/decode details, you can visit this post: Convert between UIImage and Base64 string
To encode an image:
let image = UIImage(...)
let quality = 1.0
let data: NSData = UIImageJPEGRepresentation(image, quality)!
To decode an image:
let decodedImage = UIImage(data: data)
The reason why these methods return nil for you could be that your base64string is an URL and Filename safe variant, meaning character 62 (0x3E) is replaced with a "-" (minus sign) and character 63 (0x3F) is replaced with a "_" (underscore) as noted here base64 alphabet
Try replacing character _ with / and character - with + in your string.
You could use the following code:
base64string = base64string.stringByReplacingOccurrencesOfString("-", withString: "+", options: NSStringCompareOptions.LiteralSearch, range: nil)
base64string = base64string.stringByReplacingOccurrencesOfString( "_" , withString: "/", options: NSStringCompareOptions.LiteralSearch, range: nil)
Also be aware of the correct length of the string. it has to be a multiple of 4
- take a look at this answer padded string.
NSData Class Reference : https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/index.html#//apple_ref/c/tdef/NSDataBase64DecodingOptions
NSDataBase64DecodingOptions :
struct NSDataBase64DecodingOptions : RawOptionSetType {
init(_ rawValue: UInt)
init(rawValue rawValue: UInt)
static var IgnoreUnknownCharacters: NSDataBase64DecodingOptions { get }
}
Have you tried one of the followings :
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0)!)
let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions.IgnoreUnknownCharacters)