// // MyUserIconVC.m // smartRhino // // Created by niuzhen on 2020/2/26. // Copyright © 2020 tederen. All rights reserved. // #import "MyUserIconVC.h" #import "LYLPhotoTailoringTool.h" @interface MyUserIconVC () @property (weak, nonatomic) IBOutlet UIImageView *iconView; @property (weak, nonatomic) IBOutlet UIButton *moreBtn; @end @implementation MyUserIconVC +(MyUserIconVC *)initMyUserIconVC{ MyUserIconVC *controller = [StoryboardManager.shared.myCenter instantiateViewControllerWithIdentifier:@"MyUserIconVC"]; return controller; } - (void)viewDidLoad { [super viewDidLoad]; self.fd_prefersNavigationBarHidden = YES; self.view.backgroundColor = UIColorHex(0x000000); if (self.HistoryVC) { NSString * urlString = [AppUserModel sharedAppUserModel].HistoryAvatarUrl; NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@"_50_50" withString:@""]; [self.iconView sd_setImageWithURL:[NSURL URLWithString:strUrl]]; }else{ NSString * urlString = [AppUserModel sharedAppUserModel].AvatarUrl; NSString *strUrl = [urlString stringByReplacingOccurrencesOfString:@"_50_50" withString:@""]; [self.iconView sd_setImageWithURL:[NSURL URLWithString:strUrl]]; } WS(weakSelf); [self.moreBtn setAction:^{ if (weakSelf.HistoryVC) { UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [weakSelf savePhoto]; }]; UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消"); }]; [action4 setValue:k6 forKey:@"_titleTextColor"]; [action5 setValue:k6 forKey:@"_titleTextColor"]; [actionSheet addAction:action4]; [actionSheet addAction:action5]; //相当于之前的[actionSheet show]; [weakSelf presentViewController:actionSheet animated:YES completion:nil]; }else{ UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [weakSelf selectCamera]; }]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"从手机相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [weakSelf selectPhoto]; }]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"查看上一张头像" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [weakSelf SeeUpIcon]; }]; UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"保存图片" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [weakSelf savePhoto]; }]; UIAlertAction *action5 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) { NSLog(@"取消"); }]; [action1 setValue:k6 forKey:@"_titleTextColor"]; [action2 setValue:k6 forKey:@"_titleTextColor"]; [action3 setValue:k6 forKey:@"_titleTextColor"]; [action4 setValue:k6 forKey:@"_titleTextColor"]; [action5 setValue:k6 forKey:@"_titleTextColor"]; //把action添加到actionSheet里 [actionSheet addAction:action1]; [actionSheet addAction:action2]; [actionSheet addAction:action3]; [actionSheet addAction:action4]; [actionSheet addAction:action5]; //相当于之前的[actionSheet show]; [weakSelf presentViewController:actionSheet animated:YES completion:nil]; } }]; } - (void)selectPhoto { WS(weakSelf); TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1 delegate:nil]; [imagePickerVc setNavLeftBarButtonSettingBlock:^(UIButton *leftButton){ leftButton.hidden = YES; }]; imagePickerVc.showSelectBtn = NO; imagePickerVc.allowCrop = YES; imagePickerVc.cropRect = CGRectMake(0, (SCREEN_HEIGHT - SCREEN_WIDTH) * 0.5, SCREEN_WIDTH, SCREEN_WIDTH); imagePickerVc.allowTakePicture = NO; imagePickerVc.allowTakeVideo = NO; imagePickerVc.allowPickingOriginalPhoto = NO; imagePickerVc.allowPickingGif = NO; [imagePickerVc setIsStatusBarDefault:YES]; [imagePickerVc setNaviTitleColor:[UIColor blackColor]]; [imagePickerVc setBarItemTextColor:[UIColor blackColor]]; [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray *photos, NSArray *assets, BOOL isSelectOriginalPhoto) { PHAsset *asset = assets.firstObject; switch (asset.mediaType) { case PHAssetMediaTypeVideo: { } break; case PHAssetMediaTypeImage: { UIImage * image = photos.firstObject; [[HttpManager sharedHttpManager] HeaderUploadUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserImage_Post] parameters:@{} pictureData:UIImageJPEGRepresentation(image,1.0f) pictureKey:@"file" success:^(id _Nonnull responseObject) { NSDictionary *dic = responseObject; NSLog(@"/n图片上传成功=====绝对路径=====/n%@",responseObject[@"AbsolutePath"]); NSLog(@"/n图片上传成功=====相对路径=====/n%@",responseObject[@"RelativePath"]); [UserManager updateUserInfoDetail:IMAGE Param:dic[@"RelativePath"] Success:^(id _Nonnull responseObject) { REMOVESHOW; [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,UserDetailGet] parameters:@{} success:^(id _Nonnull responseObject) { NSDictionary *dic = responseObject; [[AppUserModel sharedAppUserModel] modelSetWithDictionary:dic]; dispatch_async(dispatch_get_main_queue(), ^{ weakSelf.iconView.image = image; }); } failure:^(NSError * _Nonnull error) { }]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } break; case PHAssetMediaTypeAudio: break; case PHAssetMediaTypeUnknown: break; default: break; } }]; [self presentViewController:imagePickerVc animated:YES completion:nil]; } - (void)selectCamera { WS(weakSelf); [[LYLPhotoTailoringTool sharedTool] selectPhotoWithPhoroOrCamera:SelectPhotoWithTypePhoroOrCamera2 crop:YES showImgBlock:^(NSString *imageUrlStr) { NSLog(@"111111"); } choosImgBlock:^(UIImage *image) { NSLog(@"aaaaaa"); SHOWLOADING [[HttpManager sharedHttpManager] HeaderUploadUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,Modify_UserImage_Post] parameters:@{@"":@""} pictureData:UIImageJPEGRepresentation(image,1.0f) pictureKey:@"file" success:^(id _Nonnull responseObject) { NSDictionary *dic = responseObject; NSLog(@"/n图片上传成功=====绝对路径=====/n%@",responseObject[@"AbsolutePath"]); NSLog(@"/n图片上传成功=====相对路径=====/n%@",responseObject[@"RelativePath"]); [UserManager updateUserInfoDetail:IMAGE Param:dic[@"RelativePath"] Success:^(id _Nonnull responseObject) { REMOVESHOW; [[HttpManager sharedHttpManager] GETUrl:[NSString stringWithFormat:@"%@%@",BaseUrl,UserDetailGet] parameters:@{} success:^(id _Nonnull responseObject) { NSDictionary *dic = responseObject; [[AppUserModel sharedAppUserModel] modelSetWithDictionary:dic]; dispatch_async(dispatch_get_main_queue(), ^{ weakSelf.iconView.image = image; }); } failure:^(NSError * _Nonnull error) { }]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; } failure:^(NSError * _Nonnull error) { REMOVESHOW SHOWERROR([ZYCTool handerResultData:error]) }]; }]; } - (void)SeeUpIcon { NSString * urlString = [AppUserModel sharedAppUserModel].HistoryAvatarUrl; if (urlString.length > 0) { MyUserIconVC * vc = [MyUserIconVC initMyUserIconVC]; vc.HistoryVC = YES; [self.navigationController pushViewController:vc animated:YES]; }else{ [ShowtipTool showMessageWithStatus:@"没有历史头像"]; } } - (void)savePhoto { WS(weakSelf); if (self.iconView.image) { NSError *error = nil; [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{ [PHAssetChangeRequest creationRequestForAssetFromImage:weakSelf.iconView.image]; } error:&error]; if(error){ [ShowtipTool showMessageWithStatus:@"保存失败"]; }else{ [ShowtipTool showMessageWithStatus:@"保存成功"]; } } } @end