EditableAvatarView.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // _____ _
  2. // |_ _| |_ _ _ ___ ___ _ __ __ _
  3. // | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. // |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. //
  6. // Threema iOS Client
  7. // Copyright (c) 2015-2020 Threema GmbH
  8. //
  9. // This program is free software: you can redistribute it and/or modify
  10. // it under the terms of the GNU Affero General Public License, version 3,
  11. // as published by the Free Software Foundation.
  12. //
  13. // This program is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU Affero General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Affero General Public License
  19. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. #import <MobileCoreServices/UTCoreTypes.h>
  21. #import "EditableAvatarView.h"
  22. #import "AppDelegate.h"
  23. #import "ModalPresenter.h"
  24. #import "UIDefines.h"
  25. #import "UIImage+Resize.h"
  26. #import "RectUtil.h"
  27. #import "BundleUtil.h"
  28. #import "AvatarMaker.h"
  29. #import <RSKImageCropper/RSKImageCropper.h>
  30. @interface EditableAvatarView () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, RSKImageCropViewControllerDelegate>
  31. @property UIImagePickerController *imagePicker;
  32. @property NSInteger takePhotoIndex;
  33. @property NSInteger chooseExistingIndex;
  34. @property NSData *currentImageData;
  35. @end
  36. @implementation EditableAvatarView
  37. - (id)initWithCoder:(NSCoder *)aDecoder {
  38. self = [super initWithCoder:aDecoder];
  39. if (self) {
  40. [self setupViews];
  41. _isReceivedImage = NO;
  42. }
  43. return self;
  44. }
  45. - (instancetype)initWithFrame:(CGRect)frame {
  46. self = [super initWithFrame:frame];
  47. if (self) {
  48. [self setupViews];
  49. _isReceivedImage = NO;
  50. }
  51. return self;
  52. }
  53. - (void)setImageData:(NSData *)imageData {
  54. _currentImageData = imageData;
  55. [self updateView];
  56. }
  57. - (NSData *)imageData {
  58. return _currentImageData;
  59. }
  60. - (void)setCanDeleteImage:(BOOL)canDeleteImage {
  61. _canDeleteImage = canDeleteImage;
  62. [self updatePlaceholderView];
  63. }
  64. - (void)setCanChooseImage:(BOOL)canChooseImage {
  65. _canChooseImage = canChooseImage;
  66. [self updatePlaceholderView];
  67. }
  68. - (void)setupViews {
  69. _imageView = [[UIImageView alloc] initWithFrame:self.bounds];
  70. [self addSubview:_imageView];
  71. _pickImageLabel.userInteractionEnabled = YES;
  72. CGRect rect = CGRectInset(_imageView.frame, 10.0, 10.0);
  73. _pickImageLabel = [[UILabel alloc] initWithFrame:rect];
  74. _pickImageLabel.numberOfLines = 3;
  75. _pickImageLabel.textColor = [Colors main];
  76. _pickImageLabel.font = [UIFont systemFontOfSize:15.0];
  77. _pickImageLabel.minimumScaleFactor = 0.6;
  78. _pickImageLabel.adjustsFontSizeToFitWidth = YES;
  79. _pickImageLabel.text = NSLocalizedString(@"pick image", nil);
  80. _pickImageLabel.textAlignment = NSTextAlignmentCenter;
  81. [self addSubview:_pickImageLabel];
  82. UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage)];
  83. [_imageView addGestureRecognizer:tapRecognizer];
  84. _imageView.userInteractionEnabled = YES;
  85. UITapGestureRecognizer *tapLabelRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedImage)];
  86. [_pickImageLabel addGestureRecognizer:tapLabelRecognizer];
  87. _pickImageLabel.userInteractionEnabled = YES;
  88. if (@available(iOS 11.0, *)) {
  89. _imageView.accessibilityIgnoresInvertColors = true;
  90. }
  91. self.backgroundColor = [UIColor clearColor];
  92. [self updateView];
  93. }
  94. - (void)updateView {
  95. if (_currentImageData) {
  96. UIImage *image = [UIImage imageWithData:_currentImageData];
  97. _imageView.image = [AvatarMaker maskImage: image];
  98. _pickImageLabel.hidden = YES;
  99. } else {
  100. _imageView.image = [AvatarMaker avatarWithString:nil size:(_imageView.frame.size.height * [UIScreen mainScreen].scale)];
  101. _pickImageLabel.hidden = NO;
  102. }
  103. }
  104. - (void)updatePlaceholderView {
  105. _imageView.userInteractionEnabled = _canDeleteImage || _canChooseImage;
  106. _pickImageLabel.userInteractionEnabled = _canDeleteImage || _canChooseImage;
  107. _pickImageLabel.textColor = _canDeleteImage || _canChooseImage ? [Colors main] : [Colors fontPlaceholder];
  108. }
  109. - (void)tappedImage {
  110. if (!_canDeleteImage && !_canChooseImage) {
  111. return;
  112. }
  113. [_presentingViewController resignFirstResponder];
  114. if (!_isReceivedImage) {
  115. /* allow the user to choose a new image or clear the current one */
  116. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  117. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] && _canChooseImage) {
  118. [actionSheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"take_photo", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  119. _imagePicker = [[UIImagePickerController alloc] init];
  120. _imagePicker.delegate = self;
  121. _imagePicker.allowsEditing = NO;
  122. _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  123. _imagePicker.mediaTypes = @[(NSString*)kUTTypeImage];
  124. CGRect rect = [_presentingViewController.view convertRect:self.frame fromView:self.superview];
  125. [ModalPresenter present:_imagePicker on:_presentingViewController fromRect:rect inView:_presentingViewController.view];
  126. }]];
  127. }
  128. if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary] && _canChooseImage) {
  129. [actionSheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"choose_existing", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
  130. _imagePicker = [[UIImagePickerController alloc] init];
  131. _imagePicker.delegate = self;
  132. _imagePicker.allowsEditing = NO;
  133. _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  134. _imagePicker.mediaTypes = @[(NSString*)kUTTypeImage];
  135. CGRect rect = [_presentingViewController.view convertRect:self.frame fromView:self.superview];
  136. [ModalPresenter present:_imagePicker on:_presentingViewController fromRect:rect inView:_presentingViewController.view];
  137. }]];
  138. }
  139. if (_currentImageData != nil && _canDeleteImage) {
  140. [actionSheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"delete_photo", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction * action) {
  141. _currentImageData = nil;
  142. [self updateView];
  143. [_delegate avatarImageUpdated:_currentImageData];
  144. }]];
  145. }
  146. [actionSheet addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"cancel", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {
  147. }]];
  148. if (SYSTEM_IS_IPAD == YES || !_presentingViewController.tabBarController) {
  149. CGRect rect = [_presentingViewController.view convertRect:self.frame fromView:self.superview];
  150. actionSheet.popoverPresentationController.sourceRect = rect;
  151. actionSheet.popoverPresentationController.sourceView = self;
  152. }
  153. [[[AppDelegate sharedAppDelegate] currentTopViewController] presentViewController:actionSheet animated:YES completion:nil];
  154. }
  155. }
  156. #pragma mark - Image picker controller delegate
  157. - (void)imagePickerController:(UIImagePickerController *)_picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  158. UIImage *image = info[UIImagePickerControllerEditedImage] ?: info[UIImagePickerControllerOriginalImage];
  159. RSKImageCropViewController *imageCropViewController = [[RSKImageCropViewController alloc] initWithImage:image];
  160. imageCropViewController.delegate = self;
  161. imageCropViewController.cropMode = RSKImageCropModeCircle;
  162. imageCropViewController.avoidEmptySpaceAroundImage = YES;
  163. CGSize maskSize = CGSizeMake(kContactImageSize, kContactImageSize);
  164. imageCropViewController.preferredContentSize = CGSizeMake(maskSize.width, maskSize.height);
  165. [ModalPresenter dismissPresentedControllerOn:_presentingViewController animated:YES];
  166. CGRect rect = [_presentingViewController.view convertRect:self.frame fromView:self.superview];
  167. [ModalPresenter present:imageCropViewController on:_presentingViewController fromRect:rect inView:_presentingViewController.view];
  168. }
  169. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)_picker {
  170. [ModalPresenter dismissPresentedControllerOn:_presentingViewController animated:YES];
  171. _imagePicker = nil;
  172. [_delegate avatarImageUpdated:_currentImageData];
  173. }
  174. #pragma mark - RSKImageCropViewControllerDelegate
  175. // Crop image has been canceled.
  176. - (void)imageCropViewControllerDidCancelCrop:(RSKImageCropViewController *)controller
  177. {
  178. [ModalPresenter dismissPresentedControllerOn:_presentingViewController animated:YES];
  179. _imagePicker = nil;
  180. [_delegate avatarImageUpdated:_currentImageData];
  181. }
  182. // The original image has been cropped. Additionally provides a rotation angle used to produce image.
  183. - (void)imageCropViewController:(RSKImageCropViewController *)controller didCropImage:(UIImage *)croppedImage usingCropRect:(CGRect)cropRect rotationAngle:(CGFloat)rotationAngle
  184. {
  185. UIImage *resizedImage = [croppedImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(kContactImageSize, kContactImageSize) interpolationQuality:kCGInterpolationHigh];
  186. _currentImageData = UIImageJPEGRepresentation(resizedImage, 0.7f);
  187. [self updateView];
  188. [_delegate avatarImageUpdated:_currentImageData];
  189. [ModalPresenter dismissPresentedControllerOn:_presentingViewController animated:YES];
  190. }
  191. #pragma mark - Accessibility
  192. - (BOOL)isAccessibilityElement {
  193. return YES;
  194. }
  195. - (NSString *)accessibilityLabel {
  196. if (_currentImageData) {
  197. NSString *image = [BundleUtil localizedStringForKey:@"image"];
  198. NSString *action = [BundleUtil localizedStringForKey:@"tap_to_change"];
  199. return [NSString stringWithFormat:@"%@, %@", image, action];
  200. } else {
  201. return _pickImageLabel.text;
  202. }
  203. }
  204. @end