-2

I am trying to display a JPEG image from binary data that I am passing from a remote server. I have seen tutorials on how to convert an image already given as a JPEG to data then back to an image, but here is an issue where I have the binary data as a string. I am having trouble converting that string to the Data type so I can display it as an image. How can I convert my string to data?

My Code:

let binaryData: String = receivedMessage //receivedMessage has the 
                                         //binary data in string form
let image = UIImage(Data: receivedMessage) //error occurs here because
                                           //receivedMessage is a
                                           //String type argument

I know that in between those two lines, I need to convert the string to the Data type. How can I do that?

My data is NOT base-64 encoded, it is binary. It looks like (because it's not necessarily only ASCII values):

�|�zj\S^E#[�R��"D($�;jˊ^Tﰣu^F�q@��I$܂N�!AEI^V^Fޣ^X�Ѭ>�zBZ�!tiZ��^YqQ^R^AҔ�S�v^B3���1^Qzԥ4ْ���Ha��^@^E$�II^WI2d���j�^Cn,^HW�:��:$ @IB^ZJH^Q^Q�9E=:���-�V����J�B�^]q^M^W^\^@��1d��ƥ^RI&N<ǘ���~^B2b?x?��^@�"> ^@����������m�f��ր�^P��J�,��^E(X��$ ^@��y��}3�^@ԜG�^@}��^@YQ���^?�F^S�^@���nׄ]^Sя�t�?�=0���>k�^FT㓩Bu���b�8�Kî�蚿%5]^]ҵ)��5^]=�<�״9H�^V�ly�1�^N�� $ AJ�I) �y�1������^@?�<^/�Ծh�����Z������]eC�kU>RJ�^q��6�����,�1�^\yr}n�n*V�^S^[�B^BZ�$z��G����^?���<���s�#�B=��$ gT^TB�,�^D�XZ���^^b�7���^?턝��ֱ�,R��QR��^NhB�$������6^W�^X�xnO�F����dO���^@�y^@^K�^@�^^c�yojn��R�^@�̏W������^?�$ �mG�錩 Q�L�&H&I��7� ���0�__�^@-^]���1�=8������:=���^UVj^B|Ky ^A(��^Z@���^Y1�Z&�^[�^M^\��ƛK��Ŧ�����^^c���^@$ ��H^D�c�y�^V+��ng�r㷆�^@)��^@v~pm+�J�^@x��d��r^@ ��&~���}�h��U?���������y����g�^@��qN^G��^?�_^H²Kt�I��^W��n?N$ $�^H��c^P#�1�<�^\����^@����y�����+6^@xaN� �է���k�^W�✭Q�^L�Ћɟ���1�8�^S�g^S�^@^W?������C��^O�^Q�@ytf.�*B�+

KhanKhuu
  • 177
  • 11
  • What form is the data in the string? Is it base64 encoded? Show the first 40 or 50 characters of `receivedMessage` (update your question, do not post details in comments). – rmaddy Aug 19 '18 at 03:43
  • Probably a duplicate of https://stackoverflow.com/questions/49816253/create-uiimage-from-encoded-string – rmaddy Aug 19 '18 at 03:45
  • @rmaddy The question you offered as a possible duplicate shows x64 encoded data. That I know how to deal with. I want to skip the encoding to save time and so I just read the JPEG into a string as binary data. I am updating my question with the data now, but it will not look pretty...the binary data is not ASCII. – KhanKhuu Aug 19 '18 at 04:43
  • Please show how you get your `receivedMessage`. In my opinion, it should not be a String and you may need to fix the part you get it. – OOPer Aug 19 '18 at 04:57
  • “from binary data that I am passing from a remote server” Then what you have is Data, not a string. You must have turned it into a string yourself, in code you have not shown us. And that's the problem. – matt Aug 19 '18 at 05:08
  • Have a look at this, This may help you to solve : [convert-between-uiimage-and-base64-string](https://stackoverflow.com/questions/11251340/convert-between-uiimage-and-base64-string) – Ganesh Pawar Aug 19 '18 at 05:08
  • @matt I am reading the binary from a .jpg image in a c program on a raspberry pi. I read that data into a string and pass that string to an MQTT server that the iOS device is connected to. It receives that string from the MQTT server and now needs to display the image. I didn't think posting the c code would be relevant here, I am confident that everything in the string is the correct binary data of the JPEG – KhanKhuu Aug 19 '18 at 16:09
  • @OOPer please see my comment to matt. I use a third-party library "CocoaMQTT" to receive a message from my server. The message is in the form of a string that contains the binary data of the JPEG image. I am confident that the data is correct, I just want to see if anyone knows how to convert a string containing binary data to Data in Swift. I cannot send the data as Data type because it is coming from another program not written Swift and the only thing the MQTT server will pass is data in the form of a string. – KhanKhuu Aug 19 '18 at 16:10
  • "I am confident that everything in the string is the correct binary data of the JPEG" Stop being confident. Let go of your preconceived notions and listen to what everyone is telling you. In Swift, _a string is not binary data._ Until you get that straightened out, you can't proceed. "if anyone knows how to convert a string containing binary data to Data in Swift" There is _no such thing_ as "a string containing binary data" in Swift. – matt Aug 19 '18 at 16:23
  • @matt A string cannot hold an array of integers that represent the numeric values of my binary data? I get that a string is not binary data. But ok, let's give up on trying to "convert this string to Data". Do you have any idea of a way to take integer representation of binary data and cause Swift to treat it as such, then display an image from it? If not, can you suggest how I should ask this question properly? – KhanKhuu Aug 19 '18 at 16:24
  • That's right, it can't! Good, you're starting to get the idea. A array of integers is a Data, _not_ a String. – matt Aug 19 '18 at 16:24
  • @matt then I can create an image straight from the array of integers without converting it? UIImage(data: //insert array of integers here)? Swift does not seem to allow me to do that, which suggests that an array of integers is not a Data. Indeed, my error message states that I: cannot convert value of type [Int] to expected argument type 'Data' – KhanKhuu Aug 19 '18 at 16:27
  • 2
    But if the array of integers is an array of UInt8, which is simply bytes, you _can_ make a Data out of it. – matt Aug 19 '18 at 16:31
  • @matt That is exactly what I need to do. – KhanKhuu Aug 19 '18 at 16:37
  • 1
    Then do it! You should have typed your original array as `[UInt8]`. Now you can turn that to a Data, and you can turn that to a UIImage. Done. – matt Aug 19 '18 at 16:42

1 Answers1

2

The whole problem is caused by your insistence that a blob of binary data can be a String. It can't. It's a blob of binary data, also known as a Data.

If, for some reason, you are receiving a sequence of bytes representing an image into an array, type it as an array of UInt8. (A UInt8 is a byte.) Now you can wrap the array up as a Data and open it as an image:

let array : [UInt8] = // ... whatever ...
let d = Data(array)
let im = UIImage(data: d)
matt
  • 515,959
  • 87
  • 875
  • 1,141