XLFormImageCell.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. UIImagePickerController *imagePickerController;
  31. UIAlertController *alertController;
  32. }
  33. @end
  34. @implementation XLFormImageCell
  35. #pragma mark - XLFormDescriptorCell
  36. + (CGFloat)formDescriptorCellHeightForRowDescriptor:(XLFormRowDescriptor *)rowDescriptor
  37. {
  38. return 40;
  39. }
  40. - (void)configure
  41. {
  42. [super configure];
  43. self.selectionStyle = UITableViewCellSelectionStyleNone;
  44. self.accessoryView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
  45. self.editingAccessoryView = self.accessoryView;
  46. }
  47. - (void)update
  48. {
  49. [super update];
  50. self.textLabel.text = self.rowDescriptor.title;
  51. self.imageView.image = self.rowDescriptor.value;
  52. }
  53. - (void)chooseImage:(UIImage *)image
  54. {
  55. self.imageView.image = image;
  56. self.rowDescriptor.value = image;
  57. }
  58. - (UIImageView *)imageView
  59. {
  60. return (UIImageView *)self.accessoryView;
  61. }
  62. - (void)formDescriptorCellDidSelectedWithFormController:(XLFormViewController *)controller
  63. {
  64. alertController = [UIAlertController alertControllerWithTitle: self.rowDescriptor.title
  65. message: nil
  66. preferredStyle: UIAlertControllerStyleActionSheet];
  67. [alertController addAction:[UIAlertAction actionWithTitle: NSLocalizedString(@"Choose From Library", nil)
  68. style: UIAlertActionStyleDefault
  69. handler: ^(UIAlertAction * _Nonnull action) {
  70. [self openImage:UIImagePickerControllerSourceTypePhotoLibrary];
  71. }]];
  72. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  73. [alertController addAction:[UIAlertAction actionWithTitle: NSLocalizedString(@"Take Photo", nil)
  74. style: UIAlertActionStyleDefault
  75. handler: ^(UIAlertAction * _Nonnull action) {
  76. [self openImage:UIImagePickerControllerSourceTypeCamera];
  77. }]];
  78. }
  79. [alertController addAction:[UIAlertAction actionWithTitle: NSLocalizedString(@"Cancel", nil)
  80. style: UIAlertActionStyleCancel
  81. handler: nil]];
  82. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  83. alertController.modalPresentationStyle = UIModalPresentationPopover;
  84. alertController.popoverPresentationController.sourceView = self.contentView;
  85. alertController.popoverPresentationController.sourceRect = self.contentView.bounds;
  86. }
  87. dispatch_async(dispatch_get_main_queue(), ^{
  88. [self.formViewController presentViewController:alertController animated: true completion: nil];
  89. });
  90. }
  91. - (void)openImage:(UIImagePickerControllerSourceType)source
  92. {
  93. imagePickerController = [[UIImagePickerController alloc] init];
  94. imagePickerController.delegate = self;
  95. imagePickerController.allowsEditing = YES;
  96. imagePickerController.sourceType = source;
  97. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  98. imagePickerController.modalPresentationStyle = UIModalPresentationPopover;
  99. imagePickerController.popoverPresentationController.sourceRect = self.contentView.frame;
  100. imagePickerController.popoverPresentationController.sourceView = self.formViewController.view;
  101. imagePickerController.popoverPresentationController.permittedArrowDirections = UIPopoverArrowDirectionAny;
  102. }
  103. [self.formViewController presentViewController: imagePickerController
  104. animated: YES
  105. completion: nil];
  106. }
  107. #pragma mark - UIImagePickerControllerDelegate
  108. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
  109. {
  110. UIImage *editedImage = info[UIImagePickerControllerEditedImage];
  111. UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
  112. if (editedImage) {
  113. [self chooseImage:editedImage];
  114. } else {
  115. [self chooseImage:originalImage];
  116. }
  117. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  118. if (self.formViewController.presentedViewController && self.formViewController.presentedViewController.modalPresentationStyle == UIModalPresentationPopover) {
  119. [self.formViewController dismissViewControllerAnimated:YES completion:nil];
  120. }
  121. } else {
  122. [self.formViewController dismissViewControllerAnimated: YES completion: nil];
  123. }
  124. }
  125. @end