0

I have 5 ImageView and 5 Buttons based on that I m getting five images and add it into an array.Now I want to POST them to the server with others parameters also like userId,accessToken .

the input structure which my server wants is

{
"user_id":"26",
 "images":[{"image_id":"0","image_data":"BASE 64"}], 
 "access_token":"MQT3NaF4Njk8M3EzwEU9yUZbymM4B9NqAaSmFm8RE8l3S8JQQEpAwFEpz8lASWlUw78IwnYhSCB9R7So"
     }

i have made an DAO class in which i have created a method structure to post the data as

 -(void)userProfileImagesWithUserId:(NSInteger)userId
                        Images:(NSMutableArray *)images
                   accessToken:(NSString *)AccessToken
                 urlwithString:(NSString *)url
           withSuccessCallBack:(ResponseBlock)responseBlock
{
MGMainDAO *mainDao = [MGMainDAO new];

NSDictionary *params = @{@"user_id":[NSNumber numberWithInteger:userId],
                         @"images":[images mutableCopy],
                         @"access_token":AccessToken};




[mainDao postRequest:url withParameters:params withCompletionBlock:^(id responseData,NSError *error){

    if (!error) {

        if ([responseData isKindOfClass:[NSDictionary class]]) {


            responseBlock(YES,responseData,nil);

        }

    }else{

        responseBlock(NO,nil,error);
    }

}];

}

Now in My view Controller I m calling this method as

MG_UserMultipleImgDAO *userDao = [MG_UserMultipleImgDAO new];

  [userDao userProfileImagesWithUserId:userId Images:mainArr accessToken:accessToken urlwithString:[NSString stringWithFormat:@"%@%@",BASE_URL,userImagesPostFix] withSuccessCallBack:^(BOOL Success, NSDictionary *responseData, NSError *error)

this is not working ,It takes Images Array as nil,How can i achieve this.

I m converting my images into NSData as

for (int i = 0; i < _arrImages.count; i++) {

  NSData *base64Data = [UIImageJPEGRepresentation([_arrImages objectAtIndex:i],0.5) base64EncodedDataWithOptions:0];

    NSString* str = [[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];

    NSString *base64Image = [NSString stringWithFormat:@"(%@,%@%d),", str, @"image", i+1];

    [result appendFormat:@"%@", base64Image];

    [encodedImages addObject:result];
Nivesh
  • 201
  • 1
  • 3
  • 13
  • you can patiently format the ques before you post for better readability of coding part. – vaibhav Dec 14 '16 at 06:31
  • m working on it from 2 days . if you had some solution plz suggest me? – Nivesh Dec 14 '16 at 06:45
  • have you converted your images to base64? – Devang Tandel Dec 14 '16 at 06:48
  • yes like this for (int i = 0; i < _arrImages.count; i++) { NSData *base64Data = [UIImageJPEGRepresentation([_arrImages objectAtIndex:i],0.5) base64EncodedDataWithOptions:0]; NSString* str = [[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding]; NSString *base64Image = [NSString stringWithFormat:@"(%@,%@%d),", str, @"image", i+1]; [result appendFormat:@"%@", base64Image]; [encodedImages addObject:result]; – Nivesh Dec 14 '16 at 06:51
  • i am not getting anything here, will you please update your question with code to create dictionary – Devang Tandel Dec 14 '16 at 06:57
  • not easy to get it from comments – Devang Tandel Dec 14 '16 at 06:57
  • editing my question see – Nivesh Dec 14 '16 at 07:08
  • @"images" format in your post dictionary is not correct. – Dipankar Das Dec 14 '16 at 08:51
  • how can i correct it ....then ..?@DipankarDas – Nivesh Dec 14 '16 at 09:49
  • Perhaps sending images via a multipart request using AFNetworking may help: http://stackoverflow.com/questions/30182128/upload-multiple-images-using-afnetworking. In this case server should also be capable to handle this multipart request to store incoming data at server level. – NeverHopeless Dec 14 '16 at 10:03

2 Answers2

0

You can create your mainArr for images like this -

NSMutableArray *mainArr = [[NSMutableArray alloc]init];

    for (int i = 0; i < _arrImages.count; i++) {
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

        NSData * base64Data = [UIImageJPEGRepresentation([_arrImages objectAtIndex:i], 0.5) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
        NSString* str = [[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];

        [dict setObject:str forKey:@"image_data"];
        [dict setObject:[@(i) stringValue] forKey:@"image_id"];

        [mainArr addObject:dict];
    }

From your viewcontroller call your method as-

[userDao userProfileImagesWithUserId:userId Images:mainArr accessToken:accessToken urlwithString:[NSString stringWithFormat:@"%@%@",BASE_URL,userImagesPostFix] withSuccessCallBack:^(BOOL Success, NSDictionary *responseData, NSError *error);
Dipankar Das
  • 672
  • 5
  • 9
  • it only taking one image and m getting response as { base64 and Image Id}, i need [{"image_id":"0","image_data":"BASE 64"}], – Nivesh Dec 14 '16 at 10:14
  • check the size of _arrImages & plz show the response that you get. – Dipankar Das Dec 14 '16 at 10:18
  • yeah i have posted 2 array and got response as images = ( { "image_data" = "/9j/4AAQSkZJRgABAQAASABIAAD/4QBYRXhpZgAATU0AKgAAAAgAAgESAAMAAAAB – Nivesh Dec 14 '16 at 10:21
  • yes but it takes only one images i dont know why ..? – Nivesh Dec 14 '16 at 10:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/130593/discussion-between-dipankar-das-and-dheeraj). – Dipankar Das Dec 14 '16 at 10:28
0

Yes it's an issue with your dictionary.

This is what you passing

NSString *base64Image = [NSString stringWithFormat:@"(%@,%@%d),", str, @"image", i+1];

The code above will simply create the string, While you need two param in your array 1.Your image_id 2.Image in Base64 String

The way you can create the structure you wanted is NSDictionary, You can create it by as answer from @Dipankar Das

but still here i am avoiding to use NSMutableDictionry and will go with NSDictionary,

You can simply create NSDictionary with inside you for loop //Base64 Conversion is taken from @Dipankar Das's Answer

NSData * base64Data = [UIImageJPEGRepresentation([_arrImages objectAtIndex:i], 0.5) base64EncodedDataWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSString* str = [[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];

NSDictionary * myDict = @{@"image_data":str,@"image_id":[NSString stringWithFormat:@"%d",i],};
Devang Tandel
  • 2,988
  • 1
  • 21
  • 43