Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You first have to parse the JSON, then take the binary part, into NSData. UIImage has a method to do change NSData into image</p> <pre><code>UIImage *myimage = [UIImage imageWithData:mydata]; </code></pre> <p>Update : (hope no one minds) After making myself such a demo, NSJSONSerialization always returns strings, and after having a small back and forth conversation below. I tried to send an image as a Base64 coded string and read back in as data using base64 decode. This works as a charm.</p> <p>This is the code I use (since this works on all iOS versions, from 3.0 and up) :</p> <p>NSData+PHBase64.h</p> <pre><code>@interface NSData (PHBase64) + (id)dataWithBase64EncodedString:(NSString *)string; // Padding '=' characters are optional. Whitespace is ignored. - (NSString *)base64Encoding; @end </code></pre> <p>NSData+PHBase64.m</p> <pre><code>#import "NSData+Base64.h" static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @implementation NSData (PHBase64) + (id)dataWithBase64EncodedString:(NSString *)string; { if (string == nil) [NSException raise:NSInvalidArgumentException format:nil]; if ([string length] == 0) return [NSData data]; static char *decodingTable = NULL; if (decodingTable == NULL) { decodingTable = malloc(256); if (decodingTable == NULL) return nil; memset(decodingTable, CHAR_MAX, 256); NSUInteger i; for (i = 0; i &lt; 64; i++) decodingTable[(short)encodingTable[i]] = i; } const char *characters = [string cStringUsingEncoding:NSASCIIStringEncoding]; if (characters == NULL) // Not an ASCII string! return nil; char *bytes = malloc((([string length] + 3) / 4) * 3); if (bytes == NULL) return nil; NSUInteger length = 0; NSUInteger i = 0; while (YES) { char buffer[4]; short bufferLength; for (bufferLength = 0; bufferLength &lt; 4; i++) { if (characters[i] == '\0') break; if (isspace(characters[i]) || characters[i] == '=') continue; buffer[bufferLength] = decodingTable[(short)characters[i]]; if (buffer[bufferLength++] == CHAR_MAX) // Illegal character! { free(bytes); return nil; } } if (bufferLength == 0) break; if (bufferLength == 1) // At least two characters are needed to produce one byte! { free(bytes); return nil; } // Decode the characters in the buffer to bytes. bytes[length++] = (buffer[0] &lt;&lt; 2) | (buffer[1] &gt;&gt; 4); if (bufferLength &gt; 2) bytes[length++] = (buffer[1] &lt;&lt; 4) | (buffer[2] &gt;&gt; 2); if (bufferLength &gt; 3) bytes[length++] = (buffer[2] &lt;&lt; 6) | buffer[3]; } realloc(bytes, length); return [NSData dataWithBytesNoCopy:bytes length:length]; } - (NSString *)base64Encoding; { if ([self length] == 0) return @""; char *characters = malloc((([self length] + 2) / 3) * 4); if (characters == NULL) return nil; NSUInteger length = 0; NSUInteger i = 0; while (i &lt; [self length]) { char buffer[3] = {0,0,0}; short bufferLength = 0; while (bufferLength &lt; 3 &amp;&amp; i &lt; [self length]) buffer[bufferLength++] = ((char *)[self bytes])[i++]; // Encode the bytes in the buffer to four characters, including padding "=" characters if necessary. characters[length++] = encodingTable[(buffer[0] &amp; 0xFC) &gt;&gt; 2]; characters[length++] = encodingTable[((buffer[0] &amp; 0x03) &lt;&lt; 4) | ((buffer[1] &amp; 0xF0) &gt;&gt; 4)]; if (bufferLength &gt; 1) characters[length++] = encodingTable[((buffer[1] &amp; 0x0F) &lt;&lt; 2) | ((buffer[2] &amp; 0xC0) &gt;&gt; 6)]; else characters[length++] = '='; if (bufferLength &gt; 2) characters[length++] = encodingTable[buffer[2] &amp; 0x3F]; else characters[length++] = '='; } return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES]; } @end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload