MyUserIconVC.m 8.5 KB

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