XLFormImageCell.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // XLFormBaseCell.m
  3. // XLForm ( https://github.com/xmartlabs/XLForm )
  4. //
  5. // Copyright (c) 2015 Xmartlabs ( http://xmartlabs.com )
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining a copy
  9. // of this software and associated documentation files (the "Software"), to deal
  10. // in the Software without restriction, including without limitation the rights
  11. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the Software is
  13. // furnished to do so, subject to the following conditions:
  14. //
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. //
  18. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. // THE SOFTWARE.
  25. #import "XLFormImageCell.h"
  26. #import "XLFormRowDescriptor.h"
  27. #import "UIView+XLFormAdditions.h"
  28. @interface XLFormImageCell() <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
  29. {
  30. UIPopoverController *popoverController;
  31. UIImagePickerController *imagePickerController;
  32. UIAlertController *alertController;
  33. }
  34. @end
  35. @implementation XLFormImageCell
  36. #pragma mark - XLFormDescriptorCell
  37. + (CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor
  38. {
  39. return 40;
  40. }
  41. - (void)configure
  42. {
  43. [super configure];
  44. self.selectionStyle = UITableViewCellSelectionStyleNone;
  45. self.accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
  46. self.editingAccessoryView = self.accessoryView;
  47. }
  48. - (void)update
  49. {
  50. [super update];
  51. self.textLabel.text = self.rowDescriptor.title;
  52. self.imageView.image = self.rowDescriptor.value;
  53. }
  54. - (void)chooseImage:(UIImage *)image
  55. {
  56. self.imageView.image = image;
  57. self.rowDescriptor.value = image;
  58. }
  59. - (UIImageView *)imageView
  60. {
  61. return (UIImageView *)self.accessoryView;
  62. }
  63. - (void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
  64. {
  65. alertController = [UIAlertController alertControllerWithTitle: self.rowDescriptor.title
  66. message: nil
  67. preferredStyle: UIAlertControllerStyleActionSheet];
  68. [alertController addAction:[UIAlertAction actionWithTitle: NSLocalizedString(@"Choose From Library", nil)
  69. style: UIAlertActionStyleDefault
  70. handler: ^(UIAlertAction * _Nonnull action) {
  71. [self openImage:UIImagePickerControllerSourceTypePhotoLibrary];
  72. }]];
  73. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  74. [alertController addAction:[UIAlertAction actionWithTitle: NSLocalizedString(@"Take Photo", nil)
  75. style: UIAlertActionStyleDefault
  76. handler: ^(UIAlertAction * _Nonnull action) {
  77. [self openImage:UIImagePickerControllerSourceTypeCamera];
  78. }]];
  79. }
  80. [alertController addAction:[UIAlertAction actionWithTitle: NSLocalizedString(@"Cancel", nil)
  81. style: UIAlertActionStyleCancel
  82. handler: nil]];
  83. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  84. alertController.modalPresentationStyle = UIModalPresentationPopover;
  85. alertController.popoverPresentationController.sourceView = self.contentView;
  86. alertController.popoverPresentationController.sourceRect = self.contentView.bounds;
  87. }
  88. dispatch_async(dispatch_get_main_queue(), ^{
  89. [self.formViewController presentViewController:alertController animated: true completion: nil];
  90. });
  91. }
  92. - (void)openImage:(UIImagePickerControllerSourceType)source
  93. {
  94. imagePickerController = [[UIImagePickerController alloc] init];
  95. imagePickerController.delegate = self;
  96. imagePickerController.allowsEditing = YES;
  97. imagePickerController.sourceType = source;
  98. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  99. popoverController = [[UIPopoverController alloc] initWithContentViewController:imagePickerController];
  100. [popoverController presentPopoverFromRect: self.contentView.frame
  101. inView: self.formViewController.view
  102. permittedArrowDirections: UIPopoverArrowDirectionAny
  103. animated: YES];
  104. } else {
  105. [self.formViewController presentViewController: imagePickerController
  106. animated: YES
  107. completion: nil];
  108. }
  109. }
  110. #pragma mark - UIImagePickerControllerDelegate
  111. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
  112. {
  113. UIImage *editedImage = info[UIImagePickerControllerEditedImage];
  114. UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
  115. if (editedImage) {
  116. [self chooseImage:editedImage];
  117. } else {
  118. [self chooseImage:originalImage];
  119. }
  120. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  121. if (popoverController && popoverController.isPopoverVisible) {
  122. [popoverController dismissPopoverAnimated: YES];
  123. }
  124. } else {
  125. [self.formViewController dismissViewControllerAnimated: YES completion: nil];
  126. }
  127. }
  128. @end