How to serialize UIImage to JSON?

前端 未结 3 2120
北荒
北荒 2021-02-06 07:45

I am using

imageData = UIImagePNGRepresentation(imgvw.image);

and while posting

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


        
3条回答
  •  一整个雨季
    2021-02-06 07:55

    Try this

    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];
    

提交回复
热议问题