Ios 将NSData与NSDictionary参数一起传递到web服务
我把所有的AFNetworking服务都写在单独的web服务类中。到目前为止,通过NSDictionary参数,我一直做得很好。但现在,当我需要将NSData文件传递给web服务时,我遇到了一个问题 这里是我如何执行web服务的Ios 将NSData与NSDictionary参数一起传递到web服务,ios,objective-c,afnetworking,Ios,Objective C,Afnetworking,我把所有的AFNetworking服务都写在单独的web服务类中。到目前为止,通过NSDictionary参数,我一直做得很好。但现在,当我需要将NSData文件传递给web服务时,我遇到了一个问题 这里是我如何执行web服务的 NSData *imageData = UIImageJPEGRepresentation(self.profileImg.image, 0.5); // I need to pass this imageData NSDateFormatter *date
NSData *imageData = UIImageJPEGRepresentation(self.profileImg.image, 0.5); // I need to pass this imageData
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *stringFromDate = [dateFormatter stringFromDate:[NSDate date]];
//Encrypting Password
NSString *passwordString = _password.text;
NSString *passwordMD5 = [passwordString MD5String];
NSDictionary *params = @{@"username": _username.text,
@"password": passwordMD5,
@"email": _email.text,
@"date":stringFromDate};
WebService *serviceObj = [[WebService alloc] init];
serviceObj.delegate = self;
[serviceObj performSelectorInBackground:@selector(doRegister:) withObject:params];
NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];
NSURL *baseURL = [NSURL URLWithString:@"http://www.example.com/register.php"];
AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:imageData name:@"image" fileName:@"profile.png" mimeType:@"image/png"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[delegate didReceiveRegisterResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
下面是我如何编写web服务的
NSData *imageData = UIImageJPEGRepresentation(self.profileImg.image, 0.5); // I need to pass this imageData
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *stringFromDate = [dateFormatter stringFromDate:[NSDate date]];
//Encrypting Password
NSString *passwordString = _password.text;
NSString *passwordMD5 = [passwordString MD5String];
NSDictionary *params = @{@"username": _username.text,
@"password": passwordMD5,
@"email": _email.text,
@"date":stringFromDate};
WebService *serviceObj = [[WebService alloc] init];
serviceObj.delegate = self;
[serviceObj performSelectorInBackground:@selector(doRegister:) withObject:params];
NSMutableDictionary * parameters = [[NSMutableDictionary alloc]initWithDictionary:params];
NSURL *baseURL = [NSURL URLWithString:@"http://www.example.com/register.php"];
AFHTTPSessionManager * manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[manager POST:@"" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
[formData appendPartWithFileData:imageData name:@"image" fileName:@"profile.png" mimeType:@"image/png"];
} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
[delegate didReceiveRegisterResponse:responseObject];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
NSMutableDictionary*参数=[[NSMutableDictionary alloc]initWithDictionary:params];
NSURL*baseURL=[NSURL URLWithString:@”http://www.example.com/register.php"];
AFHTTPSessionManager*manager=[[AFHTTPSessionManager alloc]initWithBaseURL:baseURL];
manager.responseSerializer=[AFJSONResponseSerializer序列化程序];
[manager POST:@”“参数:参数constructingBodyWithBlock:^(id\u非空formData){
[formData appendPartWithFileData:imageData名称:@“image”文件名:@“profile.png”mimeType:@“image/png”];
}进度:无成功:^(NSURLSessionDataTask*\u非空任务,id\u可空响应对象){
[代表didReceiveRegisterResponse:responseObject];
}失败:^(NSURLSessionDataTask*_可空任务,NSError*_非空错误){
}];
但我现在不知道如何分配图像数据。我已经通过数组中的NSData和NSDictionary完成了分配。如下
[self performSelectorInBackground:@selector(reloadPage:)
withObject:[NSArray arrayWithObjects:pageIndex,firstCase,nil] ];
- (void) reloadPage: (NSArray *) args {
NSString *pageIndex = [args objectAtIndex:0];
NSString *firstCase = [args objectAtIndex:1];
}
从这里得到了答案