Reputation: 297
Im my iphone app i want to post image to a link. That image is selecting from phone gallery. I converted that image to nsstring. But i have an error at posting.
Error is: ( "Error Domain=org.brautaset.JSON.ErrorDomain Code=3 \"Unrecognised leading character\" UserInfo=0x9e599a0 {NSLocalizedDescription=Unrecognised leading character}" )
I'm using below codes
for converting image to string:
CGFloat compression = 0.25f;
NSData *imagedata = UIImageJPEGRepresentation(self.profileImageView.image, compression);
profileObj.profileImageString = [[NSString alloc]initWithData:imagedata encoding:NSASCIIStringEncoding];
profileObj.profileImageString = [profileObj.profileImageString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"profileObj.profileImageString: %@",profileObj.profileImageString);
for posting to link:
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
ProfileObject *profile_obj=obj;
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@upload/register/[email protected]/password/774e4b7b118994b1c58d71c088834d43ca2937623319f6b7b6c48a1846132027/iPhone/1/%@",MainUrl,profile_obj.profileImageString]];
NSLog(@"url is---%@",url);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
NSError* error = nil;
NSURLResponse* response;
NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *dataString=[[NSString alloc]initWithData:result encoding:NSUTF8StringEncoding];
NSMutableDictionary *getResponseDict = [[NSMutableDictionary alloc] init];
[getResponseDict addEntriesFromDictionary:[dataString JSONValue]];
NSString *responseStr=[getResponseDict objectForKey:@"message"];
NSLog(@"responseStr is....%@",responseStr);
return responseStr;
sorry I'm poor in english Don't mind that.
Thanks in advance
Upvotes: 0
Views: 167
Reputation: 47089
For image posting
NSString *imgPath = fullPathOfYourImage;
if([[NSFileManager defaultManager] fileExistsAtPath:imgPath])
{
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"YourImageName.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
^..YourImageName….^
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithContentsOfFile:imgPath]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
}
Upvotes: 1
Reputation: 5998
You could always base64 encode the image data.
This makes sure you got the right string in a post.
You could use extensions like this: http://svn.cocoasourcecode.com/MGTwitterEngine/NSData+Base64.m to get what you want.
Upvotes: 1