我有5个ImageView和5个按钮,基于我获得5个图像并将其添加到数组中。现在我想将它们发送到服务器,其他参数也像userId,accessToken。
我的服务器想要的输入结构是
{
"user_id":"26",
"images":[{"image_id":"0","image_data":"BASE 64"}],
"access_token":"MQT3NaF4Njk8M3EzwEU9yUZbymM4B9NqAaSmFm8RE8l3S8JQQEpAwFEpz8lASWlUw78IwnYhSCB9R7So"
}
我已经创建了一个DAO类,我在其中创建了一个方法结构来将数据发布为
-(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);
}
}];
}
现在在我的视图中,我将此方法称为
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)
这不起作用,它将Images Array视为零,我怎样才能实现这一点。
我将我的图像转换为NSData
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];
答案 0 :(得分:0)
您可以为这样的图片创建mainArr
-
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];
}
从你的viewcontroller调用你的方法 -
[userDao userProfileImagesWithUserId:userId Images:mainArr accessToken:accessToken urlwithString:[NSString stringWithFormat:@"%@%@",BASE_URL,userImagesPostFix] withSuccessCallBack:^(BOOL Success, NSDictionary *responseData, NSError *error);
答案 1 :(得分:0)
是的,你的词典存在问题。
这是你传递的内容
NSString *base64Image = [NSString stringWithFormat:@"(%@,%@%d),", str, @"image", i+1];
上面的代码只会创建字符串,而你的数组中需要两个参数 1.你的image_id 2. Base64字符串中的图像
您可以创建所需结构的方式是NSDictionary,您可以通过@Dipankar Das的答案创建它
但仍然在这里我避免使用NSMutableDictionry并将使用NSDictionary,
您可以在内部创建NSDictionary for循环 // Base64转换来自@Dipankar Das的答案
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],};