Advertisement
Guest User

Untitled

a guest
Feb 12th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. @property (strong, nonatomic) IBOutlet UIImageView *imagen;
  2. - (IBAction)btnImagen:(id)sender;
  3. - (IBAction)btnSubir:(id)sender;
  4.  
  5. - (IBAction)btnImagen:(id)sender {
  6. UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Elige" delegate:self cancelButtonTitle:@"Cancelar" destructiveButtonTitle:nil otherButtonTitles:@"Camara",@"Libreria", nil];
  7. [sheet showInView:self.view];
  8. }
  9.  
  10.  
  11. -(void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
  12. if (buttonIndex == 0) {
  13. [self iniciarCamara];
  14. }
  15. else{
  16. [self iniciarLibreria];
  17. }
  18. }
  19.  
  20.  
  21. -(void) iniciarLibreria{
  22.  
  23. UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
  24. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  25. [pickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
  26. }
  27. [pickerController setDelegate:self];
  28. [self presentModalViewController:pickerController animated:YES];
  29. }
  30.  
  31. -(void) iniciarCamara{
  32.  
  33. UIImagePickerController *pickerController = [[UIImagePickerController alloc] init];
  34. [pickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
  35. [pickerController setDelegate:self];
  36. [self presentModalViewController:pickerController animated:YES];
  37. }
  38.  
  39. -(void) imagePickerControllerDidCancel:(UIImagePickerController *)picker{
  40. NSLog(@"CANCEL");
  41. [self dismissModalViewControllerAnimated:YES];
  42. }
  43.  
  44. -(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
  45.  
  46. UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
  47. [self.imagen setImage:image];
  48. [self dismissModalViewControllerAnimated:YES];
  49.  
  50. }
  51.  
  52.  
  53. - (IBAction)btnSubir:(id)sender {
  54.  
  55. NSInteger success = 0;
  56. @try {
  57. NSString *post =[[NSString alloc] initWithFormat:@"&correo=%@",_imagen];
  58. NSLog(@"PostData: %@",post);
  59. NSURL *url=[NSURL URLWithString:@"http://larutadelserver/php/upload.php"];
  60. NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
  61. NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
  62. NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  63. [request setURL:url];
  64. [request setHTTPMethod:@"POST"];
  65. [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
  66. [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
  67. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  68. [request setHTTPBody:postData];
  69. NSError *error = [[NSError alloc] init];
  70. NSHTTPURLResponse *response = nil;
  71. NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  72. NSLog(@"Response code: %ld", (long)[response statusCode]);
  73. if ([response statusCode] >= 200 && [response statusCode] < 300)
  74. {
  75. NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
  76. NSLog(@"Response ==> %@", responseData);
  77. NSError *error = nil;
  78. NSDictionary *jsonData = [NSJSONSerialization
  79. JSONObjectWithData:urlData
  80. options:NSJSONReadingMutableContainers
  81. error:&error];
  82.  
  83. success = [jsonData[@"success"] integerValue];
  84. NSLog(@"Success: %ld",(long)success);
  85. NSString *idTaquero = [jsonData objectForKey:@"idTaquero"];
  86.  
  87. [[NSUserDefaults standardUserDefaults] setObject:idTaquero forKey:@"idTaquero"];
  88.  
  89. [[NSUserDefaults standardUserDefaults] synchronize];
  90.  
  91.  
  92. if(success == 1)
  93. {
  94.  
  95. NSLog(@"SUCCESS");
  96. } else {
  97.  
  98. NSLog(@"Error");
  99. }
  100.  
  101. }
  102. }
  103. @catch (NSException * e) {
  104. NSLog(@"Exception: %@", e);
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement