我知道之前也有人问过这个问题,但我的问题有点不同。
我想将图像上传到 PHP 服务器,并且我想从 iOS 发送更多参数和图像。 我在谷歌上搜索,找到了两种解决方案:
要么我们将以
JSON
格式的Base64
编码字符串形式发送图像。转介link .或者我们将使用表单数据将图像上传到服务器。我已经提到了这个 link .如果有人这样推荐我,请帮我在这个 API 中添加更多参数。
现在我的问题是,哪种方式是将图像上传到服务器的最佳方式,我必须在同一个 Web 服务调用中发送更多参数(用户名、密码和更多详细信息)。
提前致谢。
最佳答案
您可以通过以下两种方式将图像从 iOS 应用程序上传到 PHP 服务器
:
使用 New AFNetworking :
#import "AFHTTPRequestOperation.h"
#import "AFHTTPRequestOperationManager.h"
NSString *stringUrl =@"http://www.myserverurl.com/file/uloaddetails.php?"
NSString *string =@"http://myimageurkstrn.com/img/myimage.png"
NSURL *filePath = [NSURL fileURLWithPath:string];
NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:userid,@"id",String_FullName,@"fname",String_Email,@"emailid",String_City,@"city",String_Country,@"country",String_City,@"state",String_TextView,@"bio", nil];
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager POST:stringUrl parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
[formData appendPartWithFileURL:filePath name:@"userfile" error:nil];//here userfile is a paramiter for your image
}
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"%@",[responseObject valueForKey:@"Root"]);
Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:string delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[Alert_Success_fail show];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
Alert_Success_fail = [[UIAlertView alloc] initWithTitle:@"myappname" message:[error localizedDescription] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[Alert_Success_fail show];
}];
第二次使用NSURLConnection:
-(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];
}
这两种方式都可以很好地将图像从应用程序上传到 php 服务器,希望对您有所帮助。
关于ios - 从 iOS 上传图片到 PHP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20728317/