I want to upload image to server through post method. The format should be like ["ImageName"=Image]
. I have tried the following code but it doesn't upload the image to server. Can anyone help me ?
-(void)SaveImageToServer:(UIImage*)getImage :(NSString*)imageName :(NSString*)getUrlToSaveImg withCompletionBlock:(void(^)(NSMutableArray *))completionBlock
{
NSMutableArray *arrrrr=[[NSMutableArray alloc]init];
NSMutableURLRequest *reqqq=[[NSMutableURLRequest alloc]initWithURL: [NSURL URLWithString:getUrlToSaveImg]];
NSData *dataOfImg=UIImagePNGRepresentation(getImage);
NSString *stringImage = [dataOfImg base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString *concat=[NSString stringWithFormat:@"fileName=%@",stringImage];
NSData *dataaa = [concat dataUsingEncoding:NSUTF8StringEncoding];
[reqqq setHTTPMethod:@"POST"];
[reqqq setHTTPBody:dataaa];
NSURLSessionConfiguration *configg=[NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession*sessionn=[NSURLSession sessionWithConfiguration:configg delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *taskk=[sessionn dataTaskWithRequest:reqqq completionHandler:^(NSData *data,NSURLResponse *responce,NSError *error){
if(error)
{
NSLog(@"%@", [error localizedDescription]);
completionBlock(nil);
}else{
NSMutableDictionary *d = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments|NSJSONReadingMutableContainers error:&error];
NSLog(@"data %@",d);
if (d) {
[arrrrr addObject:d];
}
if (completionBlock) {
completionBlock(arrrrr);
}
}
}];
[task resume];
}