gpt4 book ai didi

iphone - 如何在 iOS 上将图片以 JSON 格式从图库上传到服务器

转载 作者:可可西里 更新时间:2023-11-01 17:09:07 25 4
gpt4 key购买 nike

如何将 iPhone 上图库中的图像作为 JSON 上传到服务器?我尝试了以下方法:

[Loading startLoading:YES];
NSString *urlString = [NSString stringWithFormat:@"http://railsboxtech.com/singing/jsn_song/register_response.php?accesstoken=%@",Access_Token];
//username, password, name, email, country
NSString *parameter = [NSString stringWithFormat:@"username=%@&password=%@&fname=%@&email=%@&lname=%@&btnImage4=%@",userField.text,passField.text,fnameField.text,emailField.text,lnameField.text,btnImage4];
NSConnection *conn = [[NSConnection alloc] initWithDelegate:self];
[conn sendRequest:urlString withParaMeter:parameter withMethod:@"POST" withTag:1];
[conn startAsynchronousRequest];

最佳答案

如果您想以字符串形式上传图像,您必须将图像数据转换为 Base64,然后您可以使用上述方法:-

要转换为 base64 字符串,请参阅 this Stack Overflow answer .然后您可以使用您的代码进行上传。

另一种方式

你可以像这样直接上传图片:

-(void)uploadImage
{
NSData *imageData = UIImagePNGRepresentation(yourImage);

NSString *urlString = [ NSString stringWithFormat:@"http://yourUploadImageURl.php?intid=%@",1];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", 1]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}

关于iphone - 如何在 iOS 上将图片以 JSON 格式从图库上传到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15267539/

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