Adding NSData to NSDirectory (JSON) getting invalid type in JSON write error (NSConcreteMutableData)

I am trying to add an image to my JSON file that I want to publish on the server. I am using the following code, but I keep getting the error Invalid type in JSON write (NSConcreteMutableData) . Code

 NSDictionary *parameter = [[NSDictionary alloc]init]; NSData *imageData = UIImagePNGRepresentation(imageView.image); parameter = @{@"body":@"Hi",@"image":imageData}; 

In addition, I use AFNetworking to host the image on the server.

I am not sure what is wrong with the code. Any help would be greatly appreciated

+4
source share
2 answers

If anyone else has a problem. this is the code you should use

 NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5); NSDictionary *parameter = @{@"body":@"image"}; NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:parameter constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"]; }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); }]; [httpClient enqueueHTTPRequestOperation:operation]; 

Source: Link

+3
source

try it,

 parameter = @{@"body":@"Hi",@"image":[NSString stringWithFormat:@"%@" ,imageData]}; 
+1
source

All Articles