7

I am using

imageData = UIImagePNGRepresentation(imgvw.image);

and while posting

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

after

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

now app is crashing Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid type in JSON write (NSConcreteMutableData)

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
user2181966
  • 73
  • 1
  • 4
  • 2
    Take a look here -> http://www.json.org/ It seems you need to learn more about JSON before you try to use it. The key section on this page is the **Values** section. Take a look at the kinds of values that are allowed in the JSON standard. – borrrden Aug 19 '13 at 05:16
  • What is imagedate you are setting in dictionary ? – Nishant Tyagi Aug 19 '13 at 05:17
  • I am pretty sure you are using wrong dictionary. Where have you allocated dictionary and what is imagedate here ? – Nishant Tyagi Aug 19 '13 at 05:22
  • borrrden is completely right. The NSJSONSerialization documentation also lists at the very top what kind of objects can be serialized (and NSData is not among them). – Martin R Aug 19 '13 at 05:28

3 Answers3

11

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

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

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

In a more pseudo-way 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");
    }
Community
  • 1
  • 1
CodenameLambda1
  • 1,299
  • 7
  • 17
4

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];
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
-3

you can Convert Your image in ti NSData like:-

if PNG image

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 store it in to CoreData may be like this way is usefull for you:-

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

You can load like :-

 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;
Divya Bhaloidiya
  • 5,018
  • 2
  • 25
  • 45
  • 3
    The question title is misleading. The question was how to send an image via JSON. I don't see how saving the image in Core Data helps here. – Martin R Aug 19 '13 at 05:26