2

我正在尝试发送带有多个参数的帖子并在 base64 中包含图像。

图像编码做得很好,我在在线base64到图像转换器中检查了base64图像,看起来图像编码成功。我可以毫无问题地进行后期处理,但是当我下载它时,日志显示此错误:

错误:ImageIO:JPEG 损坏的 JPEG 数据:标记 0xf1 之前的 120 个无关字节
错误: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 应用程序中测试),因此问题与服务器无关。

4

1 回答 1

0

您对您的网址进行编码,因此不需要此行:

    [requestParams stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"];

删除它并重试。

[request setURL:[NSURL URLWithString: URL_COMMENT]]; 

可以转为:

[request setURL:[NSURL URLWithString: [URL_COMMENT stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
于 2013-11-14T12:21:16.250 回答