问题描述
我使用base64编码并通过它通过JSON,生成JSON请求和呼叫我使用REST风格的RestKit API试图连接code图像。
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.
我所看到的在日志中是RestKit增加转义字符为en codeD映像,这是有效的和失败的解码图像preventing服务器结束。
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.
我想知道什么是最好的选择,从添加转义字符停止RestKit
I want to know whats the best option to stop RestKit from adding escape characters
时的例子
VpT\/X8WWDCpj1XBpJ1zPBDuwLHLnpCZgnmLX3EXaffi0p7NklAPgO7HZsmxzC\/XITc\/K4iwRSG
可以看到斜杠( \\ /
)添加到字符串。
下面是code的读音字使用编码字符串
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如下:
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];
}
我通过这个连接codeD字符串RestKit要求作为一个字符串
I am passing this encoded string to RestKit request as a string
推荐答案
由于restkit使用AFNetworking,它似乎被添加转义字符为en codeD映像我的服务器到底能不能搞定,我已经确认这是AFNetworking问题不是Restkit本身,似乎就像他们在2.0已经解决,但由于RestKit被套牢旧版本不能得到解决,最后我放弃了,选择了ASIHTTP库,并通过JSON捏造手动确保连接的base64 codeD映像不会被篡改。这解决了问题。
As restkit uses AFNetworking and it seems to be adding escape characters to encoded image my server end couldn't handle that , I have confirmed that this is AFNetworking issue not Restkit itself and seems like they have resolved it in 2.0 but since RestKit is stuck with old version it can't be solved , finally I gave up and opted for ASIHTTP library and passed JSON fabricated manually ensuring that base64 encoded image doesn't get tampered. That resolved the issue.
这篇关于从添加转义字符prevent Restkit?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!