- 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
请登录后评论
TOP
切换版块
https://ai.baidu.com/forum/topic/show/497218
自己看ai.baidu.com官网
虽然看不懂,还是向大牛敬礼
不懂啊,百度的语音文字人脸图片识别接口都开放了吗,访问量有没有限制
果然专业!
向大牛致敬!
这是我实习同事帮忙写的一个测试。前提最好还是自己会OC吧。改天我可以让我同事加一些注释。
大神! 注释再多一点点会更好,这样像我一样的菜鸟也更容易看得懂!
炒鸡详细的代码示例