Reputation: 246
well am new to iOS and i encountered a situation where i have to upload a image to server. Here is what am doing in the post method.
- (IBAction)submitClicked:(UIButton *)sender {
NSDictionary *inputData = [NSDictionary dictionaryWithObjectsAndKeys:myImage,@"coverPic", nil];
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"http://api.mapartment.in/index.php/events/create"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:70.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSData *jsonInputData = [NSJSONSerialization dataWithJSONObject:inputData options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonInputData encoding:NSUTF8StringEncoding];
[request setHTTPBody:[jsonInputString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *jsondictcity_name = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
if([[jsondictcity_name valueForKey:@"result"] isEqual:@"true"]){
NSLog(@"Success");
}
else{
NSLog(@"Try Again");
}
[self.view resignFirstResponder];
});
}
];
[postDataTask resume];
}
P.S. _localFilePath contains - /Users/appcode/Library/Developer/CoreSimulator/Devices/3C3567B8-03BC-4233-B0BC-97E3899D0AAA/data/Containers/Data/Application/3D574024-42FB-4305-807A-ACC3C128383F/Documents/png
Upvotes: 2
Views: 424
Reputation: 1821
- (IBAction)submitClicked:(UIButton *)sender {
UIImage* image = [UIImage imageWithContentsOfFile:_localFilePath];
NSString *myImage = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
NSDictionary *inputData = [NSDictionary dictionaryWithObjectsAndKeys:myImage,@"coverPic",@"POEM WRITING",@"title",@"Bring your own paper",@"desc", nil];
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"http://api.mapartment.in/index.php/events/create"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:70.0];
[request addValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSData *jsonInputData = [NSJSONSerialization dataWithJSONObject:inputData options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonInputString = [[NSString alloc] initWithData:jsonInputData encoding:NSUTF8StringEncoding];
[request setHTTPBody:[jsonInputString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
dispatch_async(dispatch_get_main_queue(), ^{
NSDictionary *jsondictcity_name = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
if([[jsondictcity_name valueForKey:@"result"] isEqual:@"true"]){
NSLog(@"Success");
}
else{
NSLog(@"Try Again");
}
[self.view resignFirstResponder];
});
}
];
[postDataTask resume];
}
Upvotes: 0
Reputation: 1306
Image will not pass with other parameter,you have to convert image into NSData
UIImage *yourImage= [UIImage imageNamed:@"image.png"];
NSData *imageData = UIImagePNGRepresentation(yourImage);
NSString *postLength = [NSString stringWithFormat:@"%d", [imageData length]];
and then pass these data in request
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody:imageData];
and other parameter pass as json.
to get UIImage from path use
UIImage *yourImage = [UIImage imageWithContentsOfFile: imageFilePath];
Upvotes: 1