WFCUMyPortraitViewController.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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/SDWebImage.h>
  11. #import "MBProgressHUD.h"
  12. #import "WFCUUtilities.h"
  13. #import "MWPhotoBrowser.h"
  14. #import "WFCUImage.h"
  15. @interface WFCUMyPortraitViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate, MWPhotoBrowserDelegate>
  16. @property (strong, nonatomic)UIImageView *portraitView;
  17. @property (nonatomic, strong)UIImage *image;
  18. @property (nonatomic, strong)WFCCUserInfo *userInfo;
  19. @end
  20. @implementation WFCUMyPortraitViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self.view setBackgroundColor:[UIColor blackColor]];
  24. self.portraitView = [[UIImageView alloc] initWithFrame:self.view.bounds];
  25. [self.view addSubview:self.portraitView];
  26. if ([[WFCCNetworkService sharedInstance].userId isEqualToString:self.userId]) {
  27. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:WFCString(@"Modify") style:UIBarButtonItemStyleDone target:self action:@selector(onModify:)];
  28. }
  29. self.userInfo = [[WFCCIMService sharedWFCIMService] getUserInfo:self.userId refresh:NO];
  30. __weak typeof(self)ws = self;
  31. [self.portraitView sd_setImageWithURL:[NSURL URLWithString:[self.userInfo.portrait stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[WFCUImage imageNamed:@"PersonalChat"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
  32. dispatch_async(dispatch_get_main_queue(), ^{
  33. if(image) {
  34. ws.image = image;
  35. } else {
  36. ws.image = [WFCUImage imageNamed:@"PersonalChat"];
  37. }
  38. });
  39. }];
  40. UITapGestureRecognizer* doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showFullScreen:)];
  41. doubleRecognizer.numberOfTapsRequired = 2;
  42. [self.portraitView addGestureRecognizer:doubleRecognizer];
  43. self.portraitView.userInteractionEnabled = YES;
  44. }
  45. - (void)showFullScreen:(id)sender {
  46. MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
  47. browser.displayActionButton = YES;
  48. browser.displayNavArrows = NO;
  49. browser.displaySelectionButtons = NO;
  50. browser.alwaysShowControls = NO;
  51. browser.zoomPhotosToFill = YES;
  52. browser.enableGrid = NO;
  53. browser.startOnGrid = NO;
  54. browser.enableSwipeToDismiss = NO;
  55. browser.autoPlayOnAppear = NO;
  56. [browser setCurrentPhotoIndex:0];
  57. [self.navigationController pushViewController:browser animated:YES];
  58. }
  59. - (void)setImage:(UIImage *)image {
  60. _image = image;
  61. if (_image.size.width) {
  62. CGRect containerRect = self.view.bounds;
  63. if (containerRect.size.width / containerRect.size.height < image.size.width / image.size.height) {
  64. 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);
  65. } else {
  66. 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);
  67. }
  68. }
  69. }
  70. - (void)onModify:(id)sender {
  71. __weak typeof(self)ws = self;
  72. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:WFCString(@"ChangePortrait") message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  73. UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:WFCString(@"Cancel") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  74. }];
  75. UIAlertAction *actionCamera = [UIAlertAction actionWithTitle:WFCString(@"TakePhotos") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  76. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  77. picker.allowsEditing = YES;
  78. picker.delegate = ws;
  79. if ([UIImagePickerController
  80. isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  81. picker.sourceType = UIImagePickerControllerSourceTypeCamera;
  82. } else {
  83. NSLog(@"无法连接相机");
  84. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  85. }
  86. [ws presentViewController:picker animated:YES completion:nil];
  87. }];
  88. UIAlertAction *actionAlubum = [UIAlertAction actionWithTitle:WFCString(@"Album") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  89. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  90. picker.allowsEditing = YES;
  91. picker.delegate = ws;
  92. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  93. [ws presentViewController:picker animated:YES completion:nil];
  94. }];
  95. //把action添加到actionSheet里
  96. [actionSheet addAction:actionCamera];
  97. [actionSheet addAction:actionAlubum];
  98. [actionSheet addAction:actionCancel];
  99. //相当于之前的[actionSheet show];
  100. [self presentViewController:actionSheet animated:YES completion:nil];
  101. }
  102. - (void)didReceiveMemoryWarning {
  103. [super didReceiveMemoryWarning];
  104. // Dispose of any resources that can be recreated.
  105. }
  106. /*
  107. #pragma mark - Navigation
  108. // In a storyboard-based application, you will often want to do a little preparation before navigation
  109. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  110. // Get the new view controller using [segue destinationViewController].
  111. // Pass the selected object to the new view controller.
  112. }
  113. */
  114. #pragma mark - UIImagePickerControllerDelegate
  115. - (void)imagePickerController:(UIImagePickerController *)picker
  116. didFinishPickingMediaWithInfo:(NSDictionary *)info {
  117. [UIApplication sharedApplication].statusBarHidden = NO;
  118. NSData *data = nil;
  119. NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
  120. if ([mediaType isEqual:@"public.image"]) {
  121. UIImage *originImage =
  122. [info objectForKey:UIImagePickerControllerEditedImage];
  123. //获取截取区域的图像
  124. UIImage *captureImage = [WFCUUtilities thumbnailWithImage:originImage maxSize:CGSizeMake(600, 600)];
  125. data = UIImageJPEGRepresentation(captureImage, 0.00001);
  126. }
  127. UIImage *previousImage = self.portraitView.image;
  128. __weak typeof(self) ws = self;
  129. __block MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  130. hud.label.text = WFCString(@"Updating");
  131. [hud showAnimated:YES];
  132. [[WFCCIMService sharedWFCIMService] uploadMedia:nil mediaData:data mediaType:Media_Type_PORTRAIT
  133. success:^(NSString *remoteUrl) {
  134. [[WFCCIMService sharedWFCIMService] modifyMyInfo:@{@(Modify_Portrait):remoteUrl} success:^{
  135. dispatch_async(dispatch_get_main_queue(), ^{
  136. [ws.portraitView sd_setImageWithURL:[NSURL URLWithString:[remoteUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:previousImage];
  137. [hud hideAnimated:YES];
  138. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  139. hud.mode = MBProgressHUDModeText;
  140. hud.label.text = WFCString(@"UpdateDone");
  141. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  142. [hud hideAnimated:YES afterDelay:1.f];
  143. });
  144. } error:^(int error_code) {
  145. dispatch_async(dispatch_get_main_queue(), ^{
  146. [hud hideAnimated:YES];
  147. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  148. hud.mode = MBProgressHUDModeText;
  149. hud.label.text = WFCString(@"UpdateFailure");
  150. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  151. [hud hideAnimated:YES afterDelay:1.f];
  152. });
  153. }];
  154. }
  155. progress:^(long uploaded, long total) {
  156. }
  157. error:^(int error_code) {
  158. dispatch_async(dispatch_get_main_queue(), ^{
  159. [hud hideAnimated:NO];
  160. [ws showHud:WFCString(@"UpdateFailure")];
  161. });
  162. }];
  163. [picker dismissViewControllerAnimated:YES completion:nil];
  164. }
  165. - (void)showHud:(NSString *)text {
  166. MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
  167. // Set the text mode to show only text.
  168. hud.mode = MBProgressHUDModeText;
  169. hud.label.text = text;
  170. // Move to bottm center.
  171. hud.offset = CGPointMake(0.f, MBProgressMaxOffset);
  172. [hud hideAnimated:YES afterDelay:1.f];
  173. }
  174. #pragma mark - MWPhotoBrowserDelegate
  175. - (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
  176. return 1;
  177. }
  178. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
  179. MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:self.userInfo.portrait]];
  180. return photo;
  181. }
  182. - (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser thumbPhotoAtIndex:(NSUInteger)index {
  183. MWPhoto *photo = [MWPhoto photoWithURL:[NSURL URLWithString:self.userInfo.portrait]];
  184. return photo;
  185. }
  186. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser didDisplayPhotoAtIndex:(NSUInteger)index {
  187. NSLog(@"Did start viewing photo at index %lu", (unsigned long)index);
  188. }
  189. - (BOOL)photoBrowser:(MWPhotoBrowser *)photoBrowser isPhotoSelectedAtIndex:(NSUInteger)index {
  190. return NO;
  191. }
  192. - (void)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index selectedChanged:(BOOL)selected {
  193. NSLog(@"Photo at index %lu selected %@", (unsigned long)index, selected ? @"YES" : @"NO");
  194. }
  195. - (void)photoBrowserDidFinishModalPresentation:(MWPhotoBrowser *)photoBrowser {
  196. // If we subscribe to this method we must dismiss the view controller ourselves
  197. NSLog(@"Did finish modal presentation");
  198. [self dismissViewControllerAnimated:YES completion:nil];
  199. }
  200. @end