Can you convert an Image to a String?

Hello, I want to know if there is the possibility to convert an image into a String and a String to an image. On this way I want to save an Image in some text file. I'm using Xcode 8.2.1 and Swift 3. Thanks.

Accepted Reply

A JPEG file contains binary data which represents an image.


So, if you have a JPEG file, you can read its contents as binary data into a Data object (that is, an instance of class Data). This is easy, because there's a Data initializer ("init(contentsOf:options:)") to read the file with a single line of code. Then you can use the Data instance method "base64EncodedString(options:)" to create a base64-encoded string representing that binary data.


That's what base64 is for — representing binary data as a string.


Later, you can retrieve this string from the text file where you stashed it, construct a new Data object using the "init(base64Encoded:options:)" initializer, and write the Data object to a new JPEG file, or use it create a UIImage/NSImage object, or whatever.


All of the nasty details of this are handled for you, because base64 is a known standard way of representing binary values in string form. You feed in data, you get a string. Later, you feed in the string, you get back data.

Replies

Do you want to convert the bitmap in a String ? Or the graphics primitives in a String ?

Have a look here :

http : / / stackoverflow.com/questions/40396110/convert-uiimage-to-base64-string-in-swift


Or do you want to save an image reference (as its URL) ?

Have you tried to simply save the image to an image file and then change the extension to .txt?

I want to convert a .jpeg image to a string, so the image as it is taken with the iPhone. I have a .csv file where I want to save the image as a string.

No, I don't have tried that yet.

This isn't going to work. The image file contents are binary, so pretending it's text means an encoding will have to be assumed, and there isn't a standard encoding that would work here.


I'm still confused about the original question, though. Are we talking about getting the text out of an image taken of text (i.e. OCR), or are we talking about converting a blob of binary values to an encoded string form? If the latter, the most straightforward way would be to convert the image file data to a Base64 string. Or convert each hex digit of the binary source to an ASCII hex character in a text representation. (The Base64 representation is probably going to be smaller, I think.)

We talking about to convert an image to a string and backwards. So like the image in binary numbers and then to a string. Maybe its easier to save the image separately. But how does it work with a base64 string? Does that gives me the image in binary numbers?

A JPEG file contains binary data which represents an image.


So, if you have a JPEG file, you can read its contents as binary data into a Data object (that is, an instance of class Data). This is easy, because there's a Data initializer ("init(contentsOf:options:)") to read the file with a single line of code. Then you can use the Data instance method "base64EncodedString(options:)" to create a base64-encoded string representing that binary data.


That's what base64 is for — representing binary data as a string.


Later, you can retrieve this string from the text file where you stashed it, construct a new Data object using the "init(base64Encoded:options:)" initializer, and write the Data object to a new JPEG file, or use it create a UIImage/NSImage object, or whatever.


All of the nasty details of this are handled for you, because base64 is a known standard way of representing binary values in string form. You feed in data, you get a string. Later, you feed in the string, you get back data.

Okay, that sounds good. Could you give me an example please, because I don't know how to do it. Thanks.

>> I don't know how to do it


I don't understand. You know how to write Swift, and you can look up the Data class at developer.apple.com, and I gave you the names of the methods, what's the problem?


OK, here's some code that implements reading the file, converting the data to a string, converting the string back to data, and creating an image:


import AppKit

let fileURL = URL (fileURLWithPath: ... some file path ...)
let fileData = try Data (contentsOf: fileURL)
let imageString = fileData.base64EncodedString ()
... and later ...
let imageData = Data (base64Encoded: imageString)!
let image = NSImage (data: imageData)!


Is that what you wanted? Is any part of that unclear?

Thank you very much. Yes, thats what I wanted.