3

I Get this kind of JSON:

NOTE: I put dots in "content" because it is too long byte array abd just to explain the situation.

{
   "id":"53abc6a7975a9c10c292f670",
   "nfcId":"testse",
   "company":"TESt",
   "qrId":"testvalue",
   "address":"ajs;ldfh",
   "mimeType":"IMAGE",
   "url":"",
   "content":"iVBORw0KGgoAAAANSUhEUgAA....."
}

And im trying to get this Json and diplay this information the "content" field has a Byte array converted on the server from Image to byte array.

I use this code in xCode to convert those bytes to NSData, then to UIImage to be able to display it in UIImageView:

    NSData *dataImage = [jsonArray[key] dataUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"data = %@", dataImage);
    UIImage *img = [UIImage imageWithData:dataImage];
    NSLog(@"img = %@", img);

The image is always gives me null.Although, data give me array of data.

I tried all kinds of encodings as a NSData parameters also:

dataUsingEncoding:NSASCIIStringEncoding
dataUsingEncoding:NSUTF8StringEncoding
dataUsingEncoding:NSUTF16StringEncoding
dataUsingEncoding:NSUTF32StringEncoding
6
  • 2
    possible duplicate of How to display a base64 image within a UIImageView? Commented Jun 27, 2014 at 7:34
  • Your content would appear to be a base-64 string representation of the image data. Commented Jun 27, 2014 at 7:35
  • it is .png image @ali59a Commented Jun 27, 2014 at 7:39
  • 2
    It's base64 encoded. Take a look at the question I linked to. Commented Jun 27, 2014 at 7:43
  • 1
    By the way, image data won't ever be encoded by any string encoding you have tried. You can't encode image data using UTF8, UTF16 or ASCII. They are String encodings, not image encodings. Commented Jun 27, 2014 at 8:08

2 Answers 2

2

I've used code like this before

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"data:image/png;base64,%@",jsonArray[key]]];
    NSData *imageData = [NSData dataWithContentsOfURL:url];
    UIImage *img = [UIImage imageWithData:imageData];

Note that initWithBase64EncodedString is only available from iOS7 onwards

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

2 Comments

But i dont always know if it is PNG or JPG. can i make it deal with all images ?
yes, it does not matter what ever the image file extension is. But before converting data to image do check if(imageData) then convert else add some dummy image depending on your requirement. If we don't check if(imageData) when there is no data app will crash because of convert null to image
1

I tried this code just right now and it works:

NSData* dataImage = [[NSData alloc] initWithBase64EncodedString:jsonArray[key] options:0];
UIImage *img = [UIImage imageWithData:dataImage];

The "content" encoded by Base64 String type.

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.