【问题标题】:image base64 corrupt post (Objective-C)图片 base64 损坏的帖子(Objective-C)
【发布时间】:2013-11-14 11:48:27
【问题描述】:

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

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

错误:ImageIO: JPEG Corrupt JPEG data: 120 extraneous bytes before 标记 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 应用中测试),因此问题与服务器无关。

【问题讨论】:

  • 你找到解决方案了吗,我遇到了同样的问题......
  • 我遇到了同样的问题。你找到解决办法了吗?

标签: objective-c post base64 corrupt-data


【解决方案1】:

你编码你的网址,所以不需要这一行:

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

删除它并重试。

还有

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

可以转为:

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

【讨论】:

  • 没用。我认为问题不在于 url,因为所有参数都没有错误地传递给服务器。但图像已损坏。你甚至可以在浏览器中看到它。
猜你喜欢
  • 1970-01-01
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
  • 2020-06-13
  • 2019-12-20
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
相关资源
最近更新 更多