Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @property (strong, nonatomic) IBOutlet UIImageView *imagen;
- - (IBAction)btnImagen:(id)sender;
- - (IBAction)btnSubir:(id)sender;
- - (IBAction)btnImagen:(id)sender {
- UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Elige" delegate:self cancelButtonTitle:@"Cancelar" destructiveButtonTitle:nil otherButtonTitles:@"Camara",@"Libreria", nil];
- [sheet showInView:self.view];
- }
- -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
- if (buttonIndex == 0) {
- [self iniciarCamara];
- }
- else{
- [self iniciarLibreria];
- }
- }
- -(void) iniciarLibreria{
- UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
- if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
- [pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
- }
- [pickerController setDelegate:self];
- [self presentModalViewController:pickerController animated:YES];
- }
- -(void) iniciarCamara{
- UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
- [pickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
- [pickerController setDelegate:self];
- [self presentModalViewController:pickerController animated:YES];
- }
- -(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
- NSLog(@"CANCEL");
- [self dismissModalViewControllerAnimated:YES];
- }
- -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
- UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
- [self.imagen setImage:image];
- [self dismissModalViewControllerAnimated:YES];
- }
- - (IBAction)btnSubir:(id)sender {
- NSInteger success = 0;
- @try {
- NSString *post =[[NSString alloc] initWithFormat:@"&correo=%@",_imagen];
- NSLog(@"PostData: %@",post);
- NSURL *url=[NSURL URLWithString:@"http://larutadelserver/php/upload.php"];
- NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
- NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
- NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
- [request setURL:url];
- [request setHTTPMethod:@"POST"];
- [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
- [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
- [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
- [request setHTTPBody:postData];
- NSError *error = [[NSError alloc] init];
- NSHTTPURLResponse *response = nil;
- NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
- NSLog(@"Response code: %ld", (long)[response statusCode]);
- if ([response statusCode] >= 200 && [response statusCode] < 300)
- {
- NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
- NSLog(@"Response ==> %@", responseData);
- NSError *error = nil;
- NSDictionary *jsonData = [NSJSONSerialization
- JSONObjectWithData:urlData
- options:NSJSONReadingMutableContainers
- error:&error];
- success = [jsonData[@"success"] integerValue];
- NSLog(@"Success: %ld",(long)success);
- NSString *idTaquero = [jsonData objectForKey:@"idTaquero"];
- [[NSUserDefaults standardUserDefaults] setObject:idTaquero forKey:@"idTaquero"];
- [[NSUserDefaults standardUserDefaults] synchronize];
- if(success == 1)
- {
- NSLog(@"SUCCESS");
- } else {
- NSLog(@"Error");
- }
- }
- }
- @catch (NSException * e) {
- NSLog(@"Exception: %@", e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement