gpt4 book ai didi

ios - 使用 NSURL 发布图像文件和字符串文本参数

转载 作者:行者123 更新时间:2023-11-29 12:11:08 31 4
gpt4 key购买 nike

我是 iOS 新手。我正在尝试使用 POST 方法发布一些数据,包括图像文件。 picture为最后一个参数

NSString *post = [[NSString alloc]initWithFormat:@"first_name=%@&last_name=%@&email=%@password=%@&picture",[_firstName text],[[_lastName text],[_email text],[_password text],[What to Add Here]];
NSLog(@"%@",post);

NSURL *url = [NSURL URLWithString:@"http://test.test.com/ws/customer/authenticate"];

//附加信息

   NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];


[request setURL:url];
[request setHTTPMethod:@"POST"];
//[request setValue:postLength forKey:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSLog(@"Response code: %ld", (long)[response statusCode]);

我可以只使用像@"demo.jpg"这样的图片路径吗?

最佳答案

一种可能的解决方案是:

UIImage *image = [UIImage imageNamed: "demo.png"];
NSData *dataImage = UIImagePNGRepresentation(image);
// request
...
[request setHTTPBody: dataImage];

在您的情况下,您可以使用 base64:

UIImage *image = [UIImage imageNamed: "demo.png"];
NSData *dataImage = UIImagePNGRepresentation(image);
NSString *stringImage = [dataImage base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *post = [[NSString alloc]initWithFormat:@"first_name=%@&last_name=%@&email=%@password=%@&picture",[_firstName text],[[_lastName text],[_email text],[_password text], stringImage];

希望对你有帮助

关于ios - 使用 NSURL 发布图像文件和字符串文本参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33411227/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com