WFCUMyPortraitViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. //
  2. // MyPortraitViewController.m
  3. // WFChat UIKit
  4. //
  5. // Created by WF Chat on 2017/10/6.
  6. // Copyright © 2017年 WildFireChat. All rights reserved.
  7. //
  8. #import "WFCUMyPortraitViewController.h"
  9. #import <WFChatClient/WFCChatClient.h>
  10. #import "SDWebImage.h"
  11. #import "MBProgressHUD.h"
  12. #import "WFCUUtilities.h"
  13. #import "SDPhotoBrowser.h"
  14. @interface WFCUMyPortraitViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, SDPhotoBrowserDelegate>
  15. @property (strong, nonatomic)UIImageView *portraitView;
  16. @property (nonatomic, strong)UIImage *image;
  17. @property (nonatomic, strong)WFCCUserInfo *userInfo;
  18. @end
  19. @implementation WFCUMyPortraitViewController
  20. - (void)viewDidLoad {
  21. [super viewDidLoad];
  22. [self.view setBackgroundColor:[UIColor blackColor]];
  23. self.portraitView = [[UIImageView alloc] initWithFrame:self.view.bounds];
  24. [self.view addSubview:self.portraitView];
  25. if ([[WFCCNetworkService sharedInstance].userId isEqualToString:self.userId]) {
  26. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Modify") style:UIBarButtonItemStyleDone target:self action:@selector(onModify:)];
  27. }
  28. self.userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.userId refresh:NO];
  29. __weak typeof(self)ws = self;
  30. [self.portraitView sd_setImageWithURL:[NSURL URLWithString:[self.userInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"PersonalChat"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  31. dispatch_async(dispatch_get_main_queue(), ^{
  32. ws.image = image;
  33. });
  34. }];
  35. UITapGestureRecognizer* doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showFullScreen:)];
  36. doubleRecognizer.numberOfTapsRequired = 2;
  37. [self.portraitView addGestureRecognizer:doubleRecognizer];
  38. self.portraitView.userInteractionEnabled = YES;
  39. }
  40. - (void)showFullScreen:(id)sender {
  41. SDPhotoBrowser *browser = [[SDPhotoBrowser alloc] init];
  42. browser.sourceImagesContainerView = self.view;
  43. browser.imageCount = 1;
  44. browser.currentImageIndex = 0;
  45. browser.delegate = self;
  46. [browser show]; // 展示图片浏览器
  47. }
  48. - (void)setImage:(UIImage *)image {
  49. _image = image;
  50. if (_image.size.width) {
  51. CGRect containerRect = self.view.bounds;
  52. if (containerRect.size.width / containerRect.size.height < image.size.width / image.size.height) {
  53. self.portraitView.frame = CGRectMake(0, (containerRect.size.height - image.size.height * containerRect.size.width/image.size.width)/2, containerRect.size.width, image.size.height * containerRect.size.width/image.size.width);
  54. } else {
  55. self.portraitView.frame = CGRectMake((containerRect.size.width - image.size.width * containerRect.size.height/image.size.height)/2, 0, image.size.width * containerRect.size.height/image.size.height, containerRect.size.height);
  56. }
  57. }
  58. }
  59. - (void)onModify:(id)sender {
  60. __weak typeof(self)ws = self;
  61. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:WFCString(@"ChangePortrait") message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  62. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:WFCString(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  63. }];
  64. UIAlertAction *actionCamera = [UIAlertAction actionWithTitle:WFCString(@"TakePhotos") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  65. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  66. picker.allowsEditing = YES;
  67. picker.delegate = ws;
  68. if ([UIImagePickerController
  69. isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  70. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  71. } else {
  72. NSLog(@"无法连接相机");
  73. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  74. }
  75. [ws presentViewController:picker animated:YES completion:nil];
  76. }];
  77. UIAlertAction *actionAlubum = [UIAlertAction actionWithTitle:WFCString(@"Album") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  78. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  79. picker.allowsEditing = YES;
  80. picker.delegate = ws;
  81. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  82. [ws presentViewController:picker animated:YES completion:nil];
  83. }];
  84. //把action添加到actionSheet里
  85. [actionSheet addAction:actionCamera];
  86. [actionSheet addAction:actionAlubum];
  87. [actionSheet addAction:actionCancel];
  88. //相当于之前的[actionSheet show];
  89. [self presentViewController:actionSheet animated:YES completion:nil];
  90. }
  91. - (void)didReceiveMemoryWarning {
  92. [super didReceiveMemoryWarning];
  93. // Dispose of any resources that can be recreated.
  94. }
  95. /*
  96. #pragma mark - Navigation
  97. // In a storyboard-based application, you will often want to do a little preparation before navigation
  98. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  99. // Get the new view controller using [segue destinationViewController].
  100. // Pass the selected object to the new view controller.
  101. }
  102. */
  103. #pragma mark - UIImagePickerControllerDelegate
  104. - (void)imagePickerController:(UIImagePickerController *)picker
  105. didFinishPickingMediaWithInfo:(NSDictionary *)info {
  106. [UIApplication sharedApplication].statusBarHidden = NO;
  107. NSData *data = nil;
  108. NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  109. if ([mediaType isEqual:@"public.image"]) {
  110. UIImage *originImage =
  111. [info objectForKey:UIImagePickerControllerEditedImage];
  112. //获取截取区域的图像
  113. UIImage *captureImage = [WFCUUtilities thumbnailWithImage:originImage maxSize:CGSizeMake(600, 600)];
  114. data = UIImageJPEGRepresentation(captureImage, 0.00001);
  115. }
  116. UIImage *previousImage = self.portraitView.image;
  117. __weak typeof(self) ws = self;
  118. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  119. hud.label.text = WFCString(@"Updating");
  120. [hud showAnimated:YES];
  121. [[WFCCIMService sharedWFCIMService] uploadMedia:nil mediaData:data mediaType:Media_Type_PORTRAIT
  122. success:^(NSString *remoteUrl) {
  123. [[WFCCIMService sharedWFCIMService] modifyMyInfo:@{@(Modify_Portrait):remoteUrl} success:^{
  124. dispatch_async(dispatch_get_main_queue(), ^{
  125. [ws.portraitView sd_setImageWithURL:[NSURL URLWithString:[remoteUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:previousImage];
  126. [hud hideAnimated:YES];
  127. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  128. hud.mode = MBProgressHUDModeText;
  129. hud.label.text = WFCString(@"UpdateDone");
  130. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  131. [hud hideAnimated:YES afterDelay:1.f];
  132. });
  133. } error:^(int error_code) {
  134. dispatch_async(dispatch_get_main_queue(), ^{
  135. [hud hideAnimated:YES];
  136. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  137. hud.mode = MBProgressHUDModeText;
  138. hud.label.text = WFCString(@"UpdateFailure");
  139. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  140. [hud hideAnimated:YES afterDelay:1.f];
  141. });
  142. }];
  143. }
  144. progress:^(long uploaded, long total) {
  145. }
  146. error:^(int error_code) {
  147. dispatch_async(dispatch_get_main_queue(), ^{
  148. [hud hideAnimated:NO];
  149. [ws showHud:WFCString(@"UpdateFailure")];
  150. });
  151. }];
  152. [picker dismissViewControllerAnimated:YES completion:nil];
  153. }
  154. - (void)showHud:(NSString *)text {
  155. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  156. // Set the text mode to show only text.
  157. hud.mode = MBProgressHUDModeText;
  158. hud.label.text = text;
  159. // Move to bottm center.
  160. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  161. [hud hideAnimated:YES afterDelay:1.f];
  162. }
  163. #pragma mark - SDPhotoBrowserDelegate
  164. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
  165. return [UIImage imageNamed:@"PersonalChat"];
  166. }
  167. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
  168. return [NSURL URLWithString:[self.userInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  169. }
  170. - (void)photoBrowserDidDismiss:(SDPhotoBrowser *)browser {
  171. }
  172. @end