0

I am using following simple code to convert Image to Base64 string in iOS

NSData *originalPhoto = UIImagePNGRepresentation([UIImage imageNamed:@"Time_Icon.png"]);
NSString *base64PhotoString = [originalPhoto base64EncodedStringWithOptions:0];

When i try to decode the string in base64PhotoString variable using one of the online Base64 image decode service image doesn't get reproduced, not sure whats going wrong. I am sending this string to a service there also image doesn't get recognised.

ok tried playing with the options and finally could get the image encoded which gets decoded using online tools properly , but still its not recognised on server side , server side programmer is using http://www.newtonsoft.com/json/help/html/Methods_T_Newtonsoft_Json_JsonSerializer.htm this library to deserialise the JSON having image in it to object , it simply ignores the image , I am using RestKit to call server side , what i have realised is RestKit added escape characters to encoded image data , can that be a problem ?

vishal dharankar
  • 7,536
  • 7
  • 57
  • 93
  • 1
    I have a category I use that's been effective in the past: https://gist.github.com/LoganWright/e4c5cbc28b05a01f6c3a – Logan Mar 27 '15 at 19:36

1 Answers1

0

Let me rephrase your question. I guess the question is you've encoded the NSData to NSString via base64 encoding, how do you retrieve it back from NSString to NSData again. If the question is like what I described, then there's already a solution on StackOverflow, and the link is here:

Base64 Decoding in iOS 7+

Nevertheless, I'll share my example code and prove it works.

- (void)viewDidLoad {
    [super viewDidLoad];

    NSData *originalPhoto = UIImagePNGRepresentation([UIImage imageNamed:@"test.png"]);
    imageOri.image = [UIImage imageWithData:originalPhoto];

    // encoding
    NSString *base64PhotoString = [originalPhoto base64EncodedStringWithOptions:0];

    // deconding
    NSData *backToData = [[NSData alloc] initWithBase64EncodedString:base64PhotoString options:0];

    imageDecode.image = [UIImage imageWithData:backToData];
}

Hope this helps

Community
  • 1
  • 1
TimeString
  • 1,778
  • 14
  • 25
  • Nope , question is exactly what you read , I tried few ways to encode image which isn't getting decoded correctly at server end that's what I was told by server programmer , to verify I used web based services and to my surprise that was true . – vishal dharankar Mar 28 '15 at 11:04