【问题标题】:POST base 64 image string to a server on iOS将 base 64 图像字符串发布到 iOS 上的服务器
【发布时间】:2015-12-03 14:31:29
【问题描述】:

据我了解,要使用本网站,我必须将图像转换为 base64 编码图像,然后将其发送到本网站。然后网站会发回一个数字(以小数形式)。

https://docs.indico.io/docs/rest-api-image-analysis

我已尝试使用多个步骤,即尝试更改用于发送文本和接收数字的类似过程。有什么建议吗?

更新:

 - (IBAction)press:(id)sender {

    //UIImage *imager = photos.image;

 NSData *imageData = UIImageJPEGRepresentation(photos.image, 1.0);
    NSString *base64Img = [imageData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];

    //not sure if @"data" or @"data.json" and whether the base64img should be behind it
    NSDictionary *parameters = @{@"data":base64Img};

    NSMutableString *parameterString = [NSMutableString string];
    for (NSString *key in [parameters allKeys]) {
        if ([parameterString length]) {
            [parameterString appendString:@"&"];
        }
        [parameterString appendFormat:@"%@=%@", key, parameters[key]];

        NSURL *url = [NSURL URLWithString:@"http://apiv2.indico.io/contentfiltering?key='17767cb46eb4b4f568832be2c953022b"];
        NSURLSession *session = [NSURLSession sharedSession];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPMethod:@"POST"];
        [request setHTTPBody:[parameterString dataUsingEncoding:NSUTF8StringEncoding]];
        NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
            if (!error) {
                if ([data length]) {
                    NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
                    //GET RESULT;
                    NSLog(@"A %@", parameters[@"results"]);

                }

            } else {
                NSLog(@"%@", error);
            }
        }];
        [task resume];

    }

我得到的结果通常返回为(null)

【问题讨论】:

  • 你到底卡在哪一部分?发布您迄今为止尝试过的任何相关代码
  • 如果你在你的请求对象创建后放置一个断点,将请求对象中的 url 复制粘贴到你的浏览器中,看看响应是否是你所期望的,否则你的 url 可能构造不正确
  • 您的请求正文似乎不是 json 格式,您可能需要 JSONSerialize 您的 parameters 字典而不是尝试对其进行 url 编码
  • @robert 我注意到您在 url(参数)中的键之前有一个单引号。那不应该在那里吧?尤其是如果没有相应的结束引号。

标签: ios objective-c base64 indico


【解决方案1】:

如果我不得不猜测,您似乎不小心在您的 API 密钥中添加了拼写错误。在上面的行中:

http://apiv2.indico.io/contentfiltering?key='17767cb46eb4b4f568832be2c953022b

应该改为:

http://apiv2.indico.io/contentfiltering?key=17767cb46eb4b4f568832be2c953022b

由于有时会引用 URL 参数,而且我对 Objective-c 的内部请求处理不是非常熟悉,我猜这会导致您看到的奇怪行为。

【讨论】:

    【解决方案2】:

    也许可以试试

    NSDictionary *parameters = @{@"data":base64Img};
    
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters
                       options:nil
                         error:&error];
    
    //do some error checking
    
    NSURL *url = [NSURL URLWithString:@"http://apiv2.indico.io/contentfiltering?key='17767cb46eb4b4f568832be2c953022b"];
    NSURLSession *session = [NSURLSession sharedSession];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:jsonData];
    
    //...
    

    只是在我的脑海中做这件事,所以可能需要一些调整

    【讨论】:

    • 我会试一试,看看会发生什么:)
    • 我在NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameters得到了一个线程1.1断点
    • 那条线上有什么有用的错误吗?看起来很奇怪
    猜你喜欢
    • 1970-01-01
    • 2017-10-24
    • 2012-08-15
    • 1970-01-01
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多