【问题标题】:How to serialize UIImage to JSON?如何将 UIImage 序列化为 JSON?
【发布时间】:2013-08-20 19:58:51
【问题描述】:

我正在使用

imageData = UIImagePNGRepresentation(imgvw.image);

在发帖时

[dic setObject:imagedata forKey:@"image"]; 

之后

NSData *data = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&theError];

由于未捕获的异常“NSInvalidArgumentException”,现在应用正在崩溃终止应用,原因:“JSON 写入中的类型无效 (NSConcreteMutableData)

【问题讨论】:

  • 看这里 -> json.org 在尝试使用它之前,您似乎需要了解更多关于 JSON 的知识。此页面上的关键部分是 Values 部分。查看 JSON 标准中允许的值类型。
  • 你在字典中设置的 imagedate 是什么?
  • 我很确定您使用了错误的字典。你在哪里分配了字典,这里的 imagedate 是什么?
  • borrrden 完全正确。 NSJSONSerialization 文档还在最顶部列出了可以序列化的对象类型(NSData 不在其中)。

标签: ios nsjsonserialization


【解决方案1】:

您需要将 UIImage 转换为 NSData,然后将该 NSData 转换为 NSString,这将是您的数据的 base64 字符串表示形式。

从 NSData* 获得 NSString* 后,您可以将其添加到您的字典中的键 @"image"

要将 NSData 转换为 base64 类型的 NSString*,请参阅以下链接: How do I do base64 encoding on iphone-sdk?

以更伪的方式,该过程将如下所示

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");
    }

【讨论】:

    【解决方案2】:

    试试这个

    NSData *imageData  = [UIImageJPEGRepresentation(self.photoImageView.image, compression)  dataUsingEncoding:NSUTF8StringEncoding];
    
    
    const unsigned char *bytes = [imageData bytes]; 
    NSUInteger length = [imageData length];
    NSMutableArray *byteArray = [NSMutableArray array];
    for (NSUInteger i = 0; i length; i++)
    {
        [byteArray addObject:[NSNumber numberWithUnsignedChar:bytes[i]]];
    }
    
    NSDictionary *dictJson = [NSDictionary dictionaryWithObjectsAndKeys:
                  byteArray, @"photo",
                  nil];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictJson options:0 error:NULL];
    

    【讨论】:

      【解决方案3】:

      您可以在 ti NSData 中转换您的图像,例如:-

      如果是 PNG 图片

      UIImage *image = [UIImage imageNamed:@"imageName.png"];
      NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
      

      如果是 JPG 图片

      UIImage *image = [UIImage imageNamed:@"imageName.jpg"];
      NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
      

      您可以将其存储到 CoreData 中,可能这种方式对您有用:-

      [newManagedObject setValue:imageData forKey:@"image"];
      

      你可以像这样加载:-

       NSManagedObject *selectedObject = [[self yourFetchCOntroller] objectAtIndexPath:indexPath];
            UIImage *image = [UIImage imageWithData:[selectedObject valueForKey:@"image"]];
      // and set this image in to your image View  
          yourimageView.image=image;
      

      【讨论】:

      • 问题标题具有误导性。问题是如何通过 JSON 发送图像。我看不出在 Core Data 中保存图像有什么帮助。
      猜你喜欢
      • 2022-01-18
      • 2021-10-02
      • 1970-01-01
      • 1970-01-01
      • 2016-05-23
      • 2018-09-08
      • 2017-02-28
      • 2011-11-30
      • 1970-01-01
      相关资源
      最近更新 更多