2

In my swift App, I need to convert Image as String, then I need to send to Server , i.e POST. Then, if I need to GET, I dont want to decode that. Response having Image file name and path.

ONline Converter:

If I convert the sample image through online converter, HERE. I am getting following string. If I pass this string to API, I can get image successfully.

Output:

iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAYAAADnRuK4AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR.... .....CIBhIIgQRCIIEQSCAQAgmEQAIhkEAIJBAIgQRCIIEQSCAEEgiEQAIhkEAIJCgN/F+AAQAXoGXl+IGgwwAAAABJRU5ErkJggg==

Through My Coding

If I convert the sample image through Coding, I am getting following string. If I pass this string to API, Successfully adding. But, I unable to view that image manually in browser too.

More Over,, Two Output Strings are not same. I dont know why? Kindly guide me, how to solve this?

Code:

var image : UIImage = UIImage(named: "home_recharge.png")!
        var imageData = UIImagePNGRepresentation(image)
        let base64String = imageData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.allZeros)
        println("base64String    \(base64String)")

Output:

iVBORw0KGgoAAAANSUhEUgAAAJAAAACQCAYAAADnRuK4AAAAAXNSR0IArs4c6QAAABxpRE9UAAAAAgAAAAAAAABIAAAAKAAAAEgAAABIAAAKygzM7MwAAAqWSURBVHgB7FwJ0JZDHC9yhJTkLuRMjuQ+opAr5zjGqAwZx...... .......FlIs5ASSDNQLA7EMtYMpBlICaQZKBYHYhlrBtIMpATSDBSLA7GMNQNpBlICaQaKxYFYxpqBNAMpgTQDxeJALGPNQJqBlECagWJxIJaxZiDNQP8B+6MkTad9NtcAAAAASUVORK5CYII=

McDonal_11
  • 3,935
  • 6
  • 24
  • 55
  • Output strings are different because online converter and `UIImagePNGRepresentation()` may use different algorithms or compression options. – mixel Oct 06 '15 at 15:26
  • What do you mean by "I unable to view that image in browser"? How do you try to do this? When you post your image to API your server must decode it from base64 and write it to file. So you can open this file and view the image. – mixel Oct 06 '15 at 15:30
  • yes u r right @mixel . But,, From response I am getting Image path as "http://192.178.2. ...... /image.jpg" – McDonal_11 Oct 07 '15 at 04:04
  • And what's the matter? I don't understand. – mixel Oct 07 '15 at 06:05
  • Bcos,, If I pass that,, iOS code string,,,, to API,,,, Response is coming,,, but Image not opening,,,,, If I opened in Browsers,,,,, Image contain errors… like that alerts are shown… But,, If I pass online converter’s String,, to API,,,,, that same image can be opened in browsers too... This is my probs. @mixel – McDonal_11 Oct 07 '15 at 06:28
  • You encode image as png with `UIImagePNGRepresentation` but server saves it with `.jpg` extension and browser tries to open image as JPEG. Replace `UIImagePNGRepresentation` with `UIImageJPEGRepresentation` or on server save image with `.png` extension. – mixel Oct 07 '15 at 06:34
  • No @mixel . Same PNG file is opening as JPEG on browser, when pass online converter's encoded string in API. – McDonal_11 Oct 07 '15 at 06:37
  • Just try it. Your current code is not correct because it opens png image as jpeg. Anyway you should fix it. The reason browser can open online converter result image despite it has wrong extension is possibly that `UIImagePNGRepresentation` strips the image of its meta data and online converter not so browser can not figure the real type of image with wrong extension without meta data. – mixel Oct 07 '15 at 06:49

1 Answers1

1

You use png image representation but server saves the image with .jpg extension so browser tries to read image as JPEG file and fails.

You should replace UIImagePNGRepresentation with UIImageJPEGRepresentation or save image with .png extension.

The reason why browser can open online converter result image despite it has wrong extension is possibly that UIImagePNGRepresentation strips the image of its meta data and online converter not so browser can not figure the real type of image with wrong extension without meta data.

mixel
  • 25,177
  • 13
  • 126
  • 165