我使用此方法将用户信息保存在数据库中。有人可以帮我上传图片吗?
- (void)Inscription:(NSArray *)value completion:(void (^)( NSString * retour))block{
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSArray *Param_header = @[@"username", @"password", @"email",@"first_name", @"last_name"];
// NSArray *Param_value = @[@"ali", @"aliiiiiiii", @"ali.ali@gmail.com",@"ali",@"zzzzz"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http:// /Messenger/services/messenger_register"]]];
NSString *aa=[self buildParameterWithPostType:@"User" andParameterHeaders:Param_header ansParameterValues:value];
[request setHTTPBody:[aa dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPMethod:@"POST"];
[NSURLConnection sendAsynchronousRequest: request
queue: queue
completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error) {
if (error || !data) {
// Handle the error
NSLog(@"Server Error : %@", error);
} else {
NSError *error = Nil;
id jsonObjects = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
block([NSString stringWithFormat:@"%@", [jsonObjects objectForKey:@"message"]]);
}
}
];
}
答案 0 :(得分:0)
您可以使用AFN等第三方框架上传图片。 以下是示例代码:
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
NSMutableDictionary *params = [NSMutableDictionary dictionary];
params[@"username"] = @"Jack";
[mgr POST:@"http:// /Messenger/services/messenger_register" parameters:params
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSString *file = [[NSBundle mainBundle] pathForResource:@"image.png" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:file];
[formData appendPartWithFileData:data name:@"file" fileName:@"image.png" mimeType:@"mage/png"];
}
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"upload success!----%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"upload failed!----%@", error);
}];