WFCUMyPortraitViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 () <UIActionSheetDelegate, 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:@"修改" 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] 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. UIActionSheet *actionSheet =
  61. [[UIActionSheet alloc] initWithTitle:@"修改头像"
  62. delegate:self
  63. cancelButtonTitle:@"取消"
  64. destructiveButtonTitle:@"拍照"
  65. otherButtonTitles:@"相册", nil];
  66. [actionSheet showInView:self.view];
  67. }
  68. - (void)didReceiveMemoryWarning {
  69. [super didReceiveMemoryWarning];
  70. // Dispose of any resources that can be recreated.
  71. }
  72. /*
  73. #pragma mark - Navigation
  74. // In a storyboard-based application, you will often want to do a little preparation before navigation
  75. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  76. // Get the new view controller using [segue destinationViewController].
  77. // Pass the selected object to the new view controller.
  78. }
  79. */
  80. #pragma mark - UIActionSheetDelegate <NSObject>
  81. - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
  82. if(buttonIndex == 0) {
  83. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  84. picker.allowsEditing = YES;
  85. picker.delegate = self;
  86. if ([UIImagePickerController
  87. isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  88. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  89. } else {
  90. NSLog(@"无法连接相机");
  91. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  92. }
  93. [self presentViewController:picker animated:YES completion:nil];
  94. } else if (buttonIndex == 1) {
  95. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  96. picker.allowsEditing = YES;
  97. picker.delegate = self;
  98. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  99. [self presentViewController:picker animated:YES completion:nil];
  100. }
  101. }
  102. #pragma mark - UIImagePickerControllerDelegate
  103. - (void)imagePickerController:(UIImagePickerController *)picker
  104. didFinishPickingMediaWithInfo:(NSDictionary *)info {
  105. [UIApplication sharedApplication].statusBarHidden = NO;
  106. NSData *data = nil;
  107. NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  108. if ([mediaType isEqual:@"public.image"]) {
  109. UIImage *originImage =
  110. [info objectForKey:UIImagePickerControllerEditedImage];
  111. //获取截取区域的图像
  112. UIImage *captureImage = [WFCUUtilities thumbnailWithImage:originImage maxSize:CGSizeMake(600, 600)];
  113. data = UIImageJPEGRepresentation(captureImage, 0.00001);
  114. }
  115. UIImage *previousImage = self.portraitView.image;
  116. __weak typeof(self) ws = self;
  117. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  118. hud.label.text = @"更新中...";
  119. [hud showAnimated:YES];
  120. [[WFCCIMService sharedWFCIMService] uploadMedia:data mediaType:Media_Type_PORTRAIT
  121. success:^(NSString *remoteUrl) {
  122. [[WFCCIMService sharedWFCIMService] modifyMyInfo:@{@(Modify_Portrait):remoteUrl} success:^{
  123. dispatch_async(dispatch_get_main_queue(), ^{
  124. [ws.portraitView sd_setImageWithURL:[NSURL URLWithString:remoteUrl] placeholderImage:previousImage];
  125. [hud hideAnimated:YES];
  126. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  127. hud.mode = MBProgressHUDModeText;
  128. hud.label.text = @"更新成功";
  129. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  130. [hud hideAnimated:YES afterDelay:1.f];
  131. });
  132. } error:^(int error_code) {
  133. dispatch_async(dispatch_get_main_queue(), ^{
  134. [hud hideAnimated:YES];
  135. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  136. hud.mode = MBProgressHUDModeText;
  137. hud.label.text = @"更新失败";
  138. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  139. [hud hideAnimated:YES afterDelay:1.f];
  140. });
  141. }];
  142. }
  143. progress:^(long uploaded, long total) {
  144. }
  145. error:^(int error_code) {
  146. dispatch_async(dispatch_get_main_queue(), ^{
  147. [hud hideAnimated:NO];
  148. [ws showHud:@"更新失败"];
  149. });
  150. }];
  151. [picker dismissViewControllerAnimated:YES completion:nil];
  152. }
  153. - (void)showHud:(NSString *)text {
  154. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  155. // Set the text mode to show only text.
  156. hud.mode = MBProgressHUDModeText;
  157. hud.label.text = text;
  158. // Move to bottm center.
  159. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  160. [hud hideAnimated:YES afterDelay:1.f];
  161. }
  162. #pragma mark - SDPhotoBrowserDelegate
  163. - (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index {
  164. return [UIImage imageNamed:@"PersonalChat"];
  165. }
  166. - (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index {
  167. return [NSURL URLWithString:self.userInfo.portrait];
  168. }
  169. - (void)photoBrowserDidDismiss:(SDPhotoBrowser *)browser {
  170. }
  171. @end