将NSData与NSDictionary参数一起传递给Web服务

时间:2016-03-26 16:17:30

标签: ios objective-c afnetworking

我将所有AFNetworking服务都写在单独的Web服务中。到目前为止,我一直在做很好的传递NSDictionary参数。但是当我需要将NSData文件传递给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];

这里我是如何编写Web服务的

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) {

}];

但我现在不知道如何分配该imagedata。

1 个答案:

答案 0 :(得分:0)

我已经完成了在数组中传递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];    
}

question

获得答案