MyUserIconVC.m 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // MyUserIconVC.m
  3. // smartRhino
  4. //
  5. // Created by niuzhen on 2020/2/26.
  6. // Copyright © 2020 tederen. All rights reserved.
  7. //
  8. #import "MyUserIconVC.h"
  9. #import "LYLPhotoTailoringTool.h"
  10. @interface MyUserIconVC ()
  11. @property (weak, nonatomic) IBOutlet UIImageView *iconView;
  12. @property (weak, nonatomic) IBOutlet UIButton *moreBtn;
  13. @end
  14. @implementation MyUserIconVC
  15. +(MyUserIconVC *)initMyUserIconVC{
  16. MyUserIconVC *controller = [StoryboardManager.shared.myCenter instantiateViewControllerWithIdentifier:@"MyUserIconVC"];
  17. return controller;
  18. }
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.fd_prefersNavigationBarHidden = YES;
  22. self.view.backgroundColor = UIColorHex(0x000000);
  23. if (self.HistoryVC) {
  24. NSString * urlString = [AppUserModel sharedAppUserModel].HistoryAvatarUrl;
  25. NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@"_50_50" withString:@""];
  26. [self.iconView sd_setImageWithURL:[NSURL URLWithString:strUrl]];
  27. }else{
  28. NSString * urlString = [AppUserModel sharedAppUserModel].AvatarUrl;
  29. NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@"_50_50" withString:@""];
  30. [self.iconView sd_setImageWithURL:[NSURL URLWithString:strUrl]];
  31. }
  32. WS(weakSelf);
  33. [self.moreBtn setAction:^{
  34. if (weakSelf.HistoryVC) {
  35. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  36. UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  37. [weakSelf savePhoto];
  38. }];
  39. UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  40. NSLog(@"取消");
  41. }];
  42. [action4 setValue:k6 forKey:@"_titleTextColor"];
  43. [action5 setValue:k6 forKey:@"_titleTextColor"];
  44. [actionSheet addAction:action4];
  45. [actionSheet addAction:action5];
  46. //相当于之前的[actionSheet show];
  47. [weakSelf presentViewController:actionSheet animated:YES completion:nil];
  48. }else{
  49. UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  50. UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  51. [weakSelf selectCamera];
  52. }];
  53. UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  54. [weakSelf selectPhoto];
  55. }];
  56. UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"查看上一张头像" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  57. [weakSelf SeeUpIcon];
  58. }];
  59. UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  60. [weakSelf savePhoto];
  61. }];
  62. UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
  63. NSLog(@"取消");
  64. }];
  65. [action1 setValue:k6 forKey:@"_titleTextColor"];
  66. [action2 setValue:k6 forKey:@"_titleTextColor"];
  67. [action3 setValue:k6 forKey:@"_titleTextColor"];
  68. [action4 setValue:k6 forKey:@"_titleTextColor"];
  69. [action5 setValue:k6 forKey:@"_titleTextColor"];
  70. //把action添加到actionSheet里
  71. [actionSheet addAction:action1];
  72. [actionSheet addAction:action2];
  73. [actionSheet addAction:action3];
  74. [actionSheet addAction:action4];
  75. [actionSheet addAction:action5];
  76. //相当于之前的[actionSheet show];
  77. [weakSelf presentViewController:actionSheet animated:YES completion:nil];
  78. }
  79. }];
  80. }
  81. - (void)selectPhoto
  82. {
  83. WS(weakSelf);
  84. TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil];
  85. [imagePickerVc setNavLeftBarButtonSettingBlock:^(UIButton *leftButton){
  86. leftButton.hidden = YES;
  87. }];
  88. imagePickerVc.showSelectBtn = NO;
  89. imagePickerVc.allowCrop = YES;
  90. imagePickerVc.cropRect = CGRectMake(0, (SCREEN_HEIGHT - SCREEN_WIDTH) * 0.5, SCREEN_WIDTH, SCREEN_WIDTH);
  91. imagePickerVc.allowTakePicture = NO;
  92. imagePickerVc.allowTakeVideo = NO;
  93. imagePickerVc.allowPickingOriginalPhoto = NO;
  94. imagePickerVc.allowPickingGif = NO;
  95. [imagePickerVc setIsStatusBarDefault:YES];
  96. [imagePickerVc setNaviTitleColor:[UIColor blackColor]];
  97. [imagePickerVc setBarItemTextColor:[UIColor blackColor]];
  98. [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
  99. PHAsset *asset = assets.firstObject;
  100. switch (asset.mediaType) {
  101. case PHAssetMediaTypeVideo: {
  102. } break;
  103. case PHAssetMediaTypeImage: {
  104. UIImage * image = photos.firstObject;
  105. [[HttpManager sharedHttpManager] HeaderUploadUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserImage_Post] parameters:@{} pictureData:UIImageJPEGRepresentation(image,1.0f) pictureKey:@"file" success:^(id _Nonnull responseObject) {
  106. NSDictionary *dic = responseObject;
  107. NSLog(@"/n图片上传成功=====绝对路径=====/n%@",responseObject[@"AbsolutePath"]);
  108. NSLog(@"/n图片上传成功=====相对路径=====/n%@",responseObject[@"RelativePath"]);
  109. [UserManager updateUserInfoDetail:IMAGE Param:dic[@"RelativePath"] Success:^(id _Nonnull responseObject) {
  110. REMOVESHOW;
  111. [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,UserDetailGet] parameters:@{} success:^(id _Nonnull responseObject) {
  112. NSDictionary *dic = responseObject;
  113. [[AppUserModel sharedAppUserModel] modelSetWithDictionary:dic];
  114. dispatch_async(dispatch_get_main_queue(), ^{
  115. weakSelf.iconView.image = image;
  116. });
  117. } failure:^(NSError * _Nonnull error) {
  118. }];
  119. } failure:^(NSError * _Nonnull error) {
  120. REMOVESHOW
  121. SHOWERROR([ZYCTool handerResultData:error])
  122. }];
  123. } failure:^(NSError * _Nonnull error) {
  124. REMOVESHOW
  125. SHOWERROR([ZYCTool handerResultData:error])
  126. }];
  127. } break;
  128. case PHAssetMediaTypeAudio:
  129. break;
  130. case PHAssetMediaTypeUnknown:
  131. break;
  132. default: break;
  133. }
  134. }];
  135. [self presentViewController:imagePickerVc animated:YES completion:nil];
  136. }
  137. - (void)selectCamera
  138. {
  139. WS(weakSelf);
  140. [[LYLPhotoTailoringTool sharedTool] selectPhotoWithPhoroOrCamera:SelectPhotoWithTypePhoroOrCamera2 crop:YES showImgBlock:^(NSString *imageUrlStr) {
  141. NSLog(@"111111");
  142. } choosImgBlock:^(UIImage *image) {
  143. NSLog(@"aaaaaa");
  144. SHOWLOADING
  145. [[HttpManager sharedHttpManager] HeaderUploadUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserImage_Post] parameters:@{@"":@""} pictureData:UIImageJPEGRepresentation(image,1.0f) pictureKey:@"file" success:^(id _Nonnull responseObject) {
  146. NSDictionary *dic = responseObject;
  147. NSLog(@"/n图片上传成功=====绝对路径=====/n%@",responseObject[@"AbsolutePath"]);
  148. NSLog(@"/n图片上传成功=====相对路径=====/n%@",responseObject[@"RelativePath"]);
  149. [UserManager updateUserInfoDetail:IMAGE Param:dic[@"RelativePath"] Success:^(id _Nonnull responseObject) {
  150. REMOVESHOW;
  151. [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,UserDetailGet] parameters:@{} success:^(id _Nonnull responseObject) {
  152. NSDictionary *dic = responseObject;
  153. [[AppUserModel sharedAppUserModel] modelSetWithDictionary:dic];
  154. dispatch_async(dispatch_get_main_queue(), ^{
  155. weakSelf.iconView.image = image;
  156. });
  157. } failure:^(NSError * _Nonnull error) {
  158. }];
  159. } failure:^(NSError * _Nonnull error) {
  160. REMOVESHOW
  161. SHOWERROR([ZYCTool handerResultData:error])
  162. }];
  163. } failure:^(NSError * _Nonnull error) {
  164. REMOVESHOW
  165. SHOWERROR([ZYCTool handerResultData:error])
  166. }];
  167. }];
  168. }
  169. - (void)SeeUpIcon
  170. {
  171. NSString * urlString = [AppUserModel sharedAppUserModel].HistoryAvatarUrl;
  172. if (urlString.length > 0) {
  173. MyUserIconVC * vc = [MyUserIconVC initMyUserIconVC];
  174. vc.HistoryVC = YES;
  175. [self.navigationController pushViewController:vc animated:YES];
  176. }else{
  177. [ShowtipTool showMessageWithStatus:@"没有历史头像"];
  178. }
  179. }
  180. - (void)savePhoto
  181. {
  182. WS(weakSelf);
  183. if (self.iconView.image) {
  184. NSError *error = nil;
  185. [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
  186. [PHAssetChangeRequest creationRequestForAssetFromImage:weakSelf.iconView.image];
  187. } error:&error];
  188. if(error){
  189. [ShowtipTool showMessageWithStatus:@"保存失败"];
  190. }else{
  191. [ShowtipTool showMessageWithStatus:@"保存成功"];
  192. }
  193. }
  194. }
  195. @end