0

I'm consuming JSON web service and I need to send image data through JOSN. The JSON schema look like below, any idea how I can achieve this?

"image":[81,
            109,
            70,
            65,
            50,
            78,
            67,
            66,
            ]

I would appreciate your help, thanks in advance.

4
  • 1
    Why not just use Base64 encoding to transmit image data? Do you have any control over the server side? Commented May 28, 2013 at 16:15
  • It's unclear what you want to achieve here. Could you describe some use-case for that? How is data coming to you? Are you asking how to parse/compress/decompress to/from some image format? Are you asking how to determine the image format? Commented May 28, 2013 at 16:19
  • @RicPerrott I have tried Base64 but It did not work, and for the server side it is already integrated with a .net app using the format above and they want to use unified service. Commented May 28, 2013 at 16:28
  • @valid I have a UIImage that I converted to NSData and I want to send it to JSON service that receive image as format I provided. Commented May 28, 2013 at 16:31

1 Answer 1

2

if you need to get an array of bytes from an Image, try this approach:

Get the data for the given image

UIImage *image = [UIImage imageNamed:@"image.png"];
NSData *data = UIImagePNGRepresentation(image);
// You can use UIImageJPEGRepresentation() if you need a jpg rather than a png

Then, use a method like this to return an NSArray of bytes

- (NSArray*) arrayOfBytesFromData:(NSData*) data
{
if (data.length > 0)
{
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:data.length];
    NSUInteger i = 0;

    for (i = 0; i < data.length; i++)
    {
        unsigned char byteFromArray = data.bytes[i];
        [array addObject:[NSValue valueWithBytes:&byteFromArray 
                                        objCType:@encode(unsigned char)]];
    }

    return [NSArray arrayWithArray:array];
}

return nil;
}

Loop through the array and form your JSON from there.

Good Luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.