How to serialize UIImage to JSON?

I use

imageData = UIImagePNGRepresentation(imgvw.image);

and upon publication

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

after

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

now application crashes Application termination due to an uncaught exception ' NSInvalidArgumentException ', reason: 'Invalid type in JSON record (NSConcreteMutableData)

+7
ios nsjsonserialization
source share
3 answers

You need to convert your UIImage to NSData, and then convert NSData to NSString, which will be a string representation of your base64 database.

Once you get NSString * from NSData *, you can add it to your dictionary in the @ "image" key

To convert NSData to base64 type NSString *, refer to the following link: How to make base64 encoding on iphone-sdk?

In a more pseudo-path, the process will look like this:

 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"); } 
+11
source share

try it

 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]; 
+4
source share

you can convert the image to ti NSData, for example: -

if the image is PNG

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

if jpg image

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

, and you can save it in CoreData, maybe this is useful for you: -

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

You can download as: -

  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; 
-3
source share

All Articles