【Object-C】直接调用百度AI-API示例
用户已被禁言 发布于2017-12 浏览:8293 回复:9
2
收藏
  • Object-C直接调用百度AI-API示例
//
//  ViewController.m
//  MyAppTest
//
//  Created by xsshome on 2017/12/26.
//  Copyright © 2017年 xsshome. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    //输入用户名的texTField
    UITextField *userNameTF;
    //输入密码的textField
    UITextField *passWordTF;
    
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    //***********post请求********************//
    NSString *testURL = @"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant?access_token=自己的token";
    testURL = [testURL  stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *url = [NSURL URLWithString:testURL];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //设置请求方法
    request.HTTPMethod = @"POST";
    //设置请求体
    UIImage  *image1 = [UIImage imageNamed:@"1"];
    NSData *imageData = UIImagePNGRepresentation(image1);
    NSString *baseStr = [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
  NSString *urlEncode=  (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
                                                                          NULL,
                                                                          (__bridge CFStringRef)baseStr,
                                                                          NULL,
                                                                          (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                                       CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding)));
    
    
    NSLog(@"urlEncode is %@",urlEncode);

    NSString *str =[NSString stringWithFormat:@"image=%@",urlEncode];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    
    request.HTTPBody = data;
    NSURLSession *session  = [NSURLSession sharedSession];
  NSURLSessionDataTask *dataTask =  [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //
        if (error) {
            NSLog(@"error is %@",error);
        }else
        {
            NSLog(@"data is %@",data);
           NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
            NSLog(@"解析得出的dic is %@",dic);
        }
    }];
    [dataTask resume];
    
    
    
    
}

//点击空白的地方可以收起键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [userNameTF resignFirstResponder];
    [passWordTF resignFirstResponder];
}

#pragma mark  NSURLSessionDelegate
//接收到服务器响应的时候调用该方法
- (void)URLSession:(NSURLSession *)session
          dataTask:(NSURLSessionDataTask *)dataTask
         didReceiveResponse:(NSURLResponse *)response
         completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler
{
    //响应头的信息
    NSLog(@"响应头 的信息%@",response);
    completionHandler(NSURLSessionResponseAllow);
}

//收到服务器的返回数据
- (void)URLSession:(NSURLSession*)session dataTask:(nonnull NSURLSessionDataTask *)dataTask didReceiveData:(nonnull NSData *)data
{
    NSLog(@"接收到的数据是%@",data);
    NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"解析得出的dic is %@",dic);
}

//请求完成的回调
- (void)URLSession:(NSURLSession *)session task:(nonnull NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error
{
    NSLog(@"请求完成");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
收藏
点赞
2
个赞
共9条回复 最后由用户已被禁言回复于2022-04
#10用户已被禁言回复于2018-04

https://ai.baidu.com/forum/topic/show/497218

1
#9用户已被禁言回复于2018-01
#7 没名字呀春天回复
不懂啊,百度的语音文字人脸图片识别接口都开放了吗,访问量有没有限制

自己看ai.baidu.com官网

0
#8daipeng_hi回复于2018-01

虽然看不懂,还是向大牛敬礼

0
#7没名字呀春天回复于2018-01

不懂啊,百度的语音文字人脸图片识别接口都开放了吗,访问量有没有限制

0
#6zr0519回复于2017-12

果然专业!

0
#5静心之王回复于2017-12

向大牛致敬!

0
#4用户已被禁言回复于2017-12
#3 yezi199229回复
大神! 注释再多一点点会更好,这样像我一样的菜鸟也更容易看得懂!

这是我实习同事帮忙写的一个测试。前提最好还是自己会OC吧。改天我可以让我同事加一些注释。

1
#3yezi199229回复于2017-12

大神! 注释再多一点点会更好,这样像我一样的菜鸟也更容易看得懂!

0
#2周俊316回复于2017-12

炒鸡详细的代码示例

1
TOP
切换版块