I am trying to encode image using base64 encoding and pass it through JSON , to generate JSON request and call RESTful API I am using RestKit.
What I have seen in the log is RestKit adds escape characters to encoded image, this is preventing server end from decoding image effectively and fails.
I want to know whats the best option to stop RestKit from adding escape characters
below is the example
VpT\/X8WWDCpj1XBpJ1zPBDuwLHLnpCZgnmLX3EXaffi0p7NklAPgO7HZsmxzC\/XITc\/K4iwRSG
One can see slashes (\/
) added to the string.
Here is the code that i m using for encoding string
NSData *originalPhoto = UIImagePNGRepresentation([UIImage imageNamed:@"Time_Icon.png"]);
NSString *base64PhotoString = [Base64 encode:originalPhoto];
Base64.m as follows
+ (NSString*) encode:(const uint8_t*) input length:(NSInteger) length {
NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t* output = (uint8_t*)data.mutableBytes;
for (NSInteger i = 0; i < length; i += 3) {
NSInteger value = 0;
for (NSInteger j = i; j < (i + 3); j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger index = (i / 3) * 4;
output[index + 0] = encodingTable[(value >> 18) & 0x3F];
output[index + 1] = encodingTable[(value >> 12) & 0x3F];
output[index + 2] = (i + 1) < length ? encodingTable[(value >> 6) & 0x3F] : '=';
output[index + 3] = (i + 2) < length ? encodingTable[(value >> 0) & 0x3F] : '=';
}
return [[[NSString alloc] initWithData:data
encoding:NSASCIIStringEncoding] autorelease];
}
+ (NSString*) encode:(NSData*) rawBytes {
return [self encode:(const uint8_t*) rawBytes.bytes length:rawBytes.length];
}
I am passing this encoded string to RestKit request as a string