Search code examples
iosobjective-cjsonuiimagensdata

Proper way to get the byte array from the JSON


I am trying to get the image from the byte array. I can only get the image if enter the byte array values into the string directly as follows:

    NSMutableString *imagen = [[NSMutableString alloc] initWithString:@"-1,-40,-1,-32,0,16,74,70,73,70,0,1,0,1,0,96,0,96,0,0,-1,-2,0,31,76,69,65,68,32,84,101,99,104,110,111,108,111,103,105,101,115,32,73,110,99,46,32,86,49,46,48,49,0,-1,-37,0,-124,0,5,5,5,8,5,8,12,7,7,12,12,9,9,9,12,13,12,12,12,12,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13,13];

//this way works fine

IF i try to get the byte array into string as follows,then I couldnt get the image:

    NSString *logo=[NSString stringWithFormat:@"%@",[[JSON valueForKey:@"request"]valueForKey:@"logo" ]];

    NSMutableString *imagen = [[NSMutableString alloc] initWithString:logo];

   //this way doesnt work out

I am following this link to get the thing done.this link

Could you please tell me whats the proper way to get byte array from JSON?


Solution

  • This is not executable code, it is a method to use:

    You get the JSON in self.receivedData

    Convert it into an object with
    NSDictionary *jsonObject = NSJSONSerialization JSONObjectWithData:

    ** Unknown how the image data is encoded in the JSON. If it is Base64 encoded:

    Get the Base64image string with
    NSString *imageString = jsonObject[@"request"][@"logo"]

    Convert the Base64 string into data:
    NSData *imageData = [NSData alloc] initWithBase64EncodedString: imageString options:

    Get an image with
    UIImage *logoImage = [imageData imageWithData]

    All in all you have way to much code that accomplished nothing and converting the image data to an NSString is incorrect.

    Converting to a string and then back to data accomplishes nothing.