Hello and greetings. I constructed the following block of code to upload an image file to some location on the server but when built, it gives me error 'too many arguments to function StringWithString'. I am totally new to Xcode and Objective C. So I haven't got much clue as to what I am doing wrong. Please help me by pinpointing the seource of error and rectify the situation.
Here is my block of code.
- (IBAction)uploadImage {
NSDateFormatter *format=[[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyyMMddHHmmss"];
NSDate *now=[[NSDate alloc] init];
NSString *imageName=[NSString stringWithFormat:@"Image_%@", [format stringFromDate:now]];
[now release];
[format release];
NSData *imageData = UIImageJPEGRepresentation(image.image, 90);
NSString *urlString = @"
http://abc.com/page.php";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", imageName] dataUsingEncoding:NSUTF8StringEncoding]]; <----------- this line gives the error<br /> [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(returnString);
}
Look forward to some active help on this one. Any kind of assistance will be immensely appreciated.
Many Thanks
Replies
EDIT: appdesignify's method is smarter, use StringWithFormat. But still don't try to do everything on one line.
- Spam
- Abuse
- Troll
1 • Off Topic Insightful Disagree Dislike 1Like AwesomeiPhone App Templates
app templates
- Spam
- Abuse
- Troll
1 • Off Topic Insightful Disagree Dislike 1Like AwesomeMany Thanks.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeThe simulator is running on your Mac so it has a lot more memory, is much faster, and there can be plenty of issues that you will encounter on an actual device that you will never find using the simulator to test.
iPhone App Templates
app templates
- Spam
- Abuse
- Troll
1 • Off Topic Insightful Disagree Dislike 1Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeDuncan C
WareTo
Animated GIF created with Face Dancer, available for free in the app store.
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeimagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
}
else
{
imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
imagePickerController.allowsEditing = NO;
imagePickerController.delegate = self;
[self presentViewController:imagePickerController animated:YES completion:Nil];
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome