我正在尝试发送包含多个参数的帖子,并在base64中包含图片。
图像编码做得很好,我检查了在线base64到图像转换器中的base64图像,看起来图像编码成功。我可以毫无问题地进行后期处理,但是当我下载它时,日志会显示以下错误:
错误:ImageIO:JPEG损坏的JPEG数据:之前的120个外部字节 标记0xf1
错误:ImageIO:JPEG不支持的标记类型0xf1
我在方法
中这样做jpgData = UIImageJPEGRepresentation(image, 0.1f);
imageString = [jpgData base64EncodedStringWithOptions:0];
这是发送帖子的方法,这是我认为错误的地方。
- (void)putComment{
dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);
__block int responseCode = 0;
dispatch_async(backgroundQueue, ^{
NSString *requestParams = [NSString stringWithFormat:
@"idAdvertiser=%@&idUserDevice=%@&image=%@&text=%@&userName=%@&groups=%@",
ADVERTISER_ID, idUserDevice, imageString, texto, userName, groups];
[requestParams stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
NSData *postData = [requestParams dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
// NSData *postData = [requestParams dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLenght = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [NSMutableURLRequest new];
[request setURL:[NSURL URLWithString: URL_COMMENT]];
[request setHTTPMethod:@"POST"];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setValue:postLenght forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
[request setTimeoutInterval:40];
NSError *error = nil;
NSHTTPURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest: request
returningResponse: &response
error: &error];
});
}
服务器端工作正常(在Android应用程序中测试),因此问题与服务器无关。
答案 0 :(得分:0)
您对网址进行编码,因此不需要此行:
[requestParams stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];
将其删除,然后重试。
另外
[request setURL:[NSURL URLWithString: URL_COMMENT]];
可以转为:
[request setURL:[NSURL URLWithString: [URL_COMMENT stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];