Ios AFNetworking结合数据和图像发送到PHP

Ios AFNetworking结合数据和图像发送到PHP,ios,objective-c,afnetworking,Ios,Objective C,Afnetworking,我(终于)在iOS项目中使用afnetworking。我已经成功地将POST数据和图像发送到我的php服务器,但是是在单独的代码块中 发送数据: NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:myname,@"name", mysencondname, @"twoname", nil]; NSURL *url = [NSURL URLWithString:@"http://DOMAIN.COM"];

我(终于)在iOS项目中使用afnetworking。我已经成功地将
POST
数据和图像发送到我的php服务器,但是是在单独的代码块中

发送数据:

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:myname,@"name", mysencondname, @"twoname", nil];

NSURL *url = [NSURL URLWithString:@"http://DOMAIN.COM"];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient defaultValueForHeader:@"Accept"];

[httpClient postPath:@"post.php" parameters:parameters
             success:^(AFHTTPRequestOperation *operation, id response) {
                        NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
                        [self requestDone:[operation.response statusCode]];
                     }
             failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                        NSLog(@"Error with request");
                        NSLog(@"%@",[error localizedDescription]);
                        [self requestFailed];
}];
NSData *imageToUpload = UIImagePNGRepresentation(_imageView.image);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.DOMAIN.com"]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"avatar" fileName:fullFilename mimeType:@"image/png"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *response = [operation responseString];
    NSLog(@"response: [%@]",response);
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [self dismissViewControllerAnimated:YES completion:NULL];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if([operation.response statusCode] == 403){
        NSLog(@"Upload Failed");
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }
    NSLog(@"error: %@", [operation error]);

}];
发送图像:

NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:myname,@"name", mysencondname, @"twoname", nil];

NSURL *url = [NSURL URLWithString:@"http://DOMAIN.COM"];

AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
[httpClient defaultValueForHeader:@"Accept"];

[httpClient postPath:@"post.php" parameters:parameters
             success:^(AFHTTPRequestOperation *operation, id response) {
                        NSLog(@"operation hasAcceptableStatusCode: %d", [operation.response statusCode]);
                        [self requestDone:[operation.response statusCode]];
                     }
             failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                        NSLog(@"Error with request");
                        NSLog(@"%@",[error localizedDescription]);
                        [self requestFailed];
}];
NSData *imageToUpload = UIImagePNGRepresentation(_imageView.image);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.DOMAIN.com"]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"avatar" fileName:fullFilename mimeType:@"image/png"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *response = [operation responseString];
    NSLog(@"response: [%@]",response);
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    [self dismissViewControllerAnimated:YES completion:NULL];

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if([operation.response statusCode] == 403){
        NSLog(@"Upload Failed");
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        return;
    }
    NSLog(@"error: %@", [operation error]);

}];
NSData*imageToUpload=UIImagePNGRepresentation(_imageView.image);
AFHTTPClient*客户端=[AFHTTPClient客户端WithBaseURL:[NSURL URLWithString:@]http://www.DOMAIN.com"]];
NSMutableURLRequest*request=[客户端multipartFormRequestWithMethod:@“POST”路径:@“upload.php”参数:nil constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:ImageToPload名称:@“avatar”文件名:fullFilename mimeType:@“image/png”];
}];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:request];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象){
NSString*response=[operation responseString];
NSLog(@“响应:[%@]”,响应);
[MBProgressHUD hideHUDForView:self.view动画:是];
[self-dismissViewControllerAnimated:是完成:空];
}失败:^(AFHTTPRequestOperation*操作,NSError*错误){
如果([operation.response statusCode]==403){
NSLog(@“上传失败”);
[MBProgressHUD hideHUDForView:self.view动画:是];
返回;
}
NSLog(@“错误:%@,[操作错误]);
}];
我的问题是如何将它们组合起来,以便通过单个请求将图像和一些数据(如(
myname、@“name”、MyEncondName、@“twoname”
)发送到我的服务器?

在multipartFormRequestWithMethod方法中,有一个称为参数的参数,可以用来发送一些数据

例如:

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                            myName, @"name"
                            , secondName, @"twoname"
                            , nil];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:params constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"avatar" fileName:fullFilename mimeType:@"image/png"];
}];
NSDictionary*参数=[NSDictionary Dictionary WithObjectsAndKeys:
我的名字,@“名字”
,secondName,@“twoname”
,无];
NSMutableURLRequest*request=[客户端multipartFormRequestWithMethod:@“POST”路径:@“upload.php”参数:params constructingBodyWithBlock:^(id formData){
[formData appendPartWithFileData:ImageToPload名称:@“avatar”文件名:fullFilename mimeType:@“image/png”];
}];

我会试试那个,然后告诉你。谢谢!我得到了它!非常感谢你!