我在用
imageData = UIImagePNGRepresentation(imgvw.image);
而在发布时
[dic setObject:imagedata forKey:@"image"];
后
NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&theError];
现在由于未捕获的异常' NSInvalidArgumentException
',应用程序崩溃正在终止应用程序,原因:'JSON写入中的类型无效(NSConcreteMutableData)
您需要将UIImage转换为NSData,然后将该NSData转换为NSString,它将成为数据的base64字符串表示形式。
从NSData *获取NSString *后,您可以将其添加到字典中的键@“ image”
要将NSData转换为base64类型的NSString *,请参考以下链接:如何在iphone-sdk上进行base64编码?
用更伪的方式,过程看起来像这样
UIImage *my_image; //your image handle
NSData *data_of_my_image = UIImagePNGRepresentation(my_image);
NSString *base64StringOf_my_image = [data_of_my_image convertToBase64String];
//now you can add it to your dictionary
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:base64StringOf_my_image forKey:@"image"];
if ([NSJSONSerialization isValidJSONObject:dict]) //perform a check
{
NSLog(@"valid object for JSON");
NSError *error = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
if (error!=nil) {
NSLog(@"Error creating JSON Data = %@",error);
}
else{
NSLog(@"JSON Data created successfully.");
}
}
else{
NSLog(@"not a valid object for JSON");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句