GetHeadImgViewController.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // GetHeadImgViewController.m
  3. // BuguLive
  4. //
  5. // Created by fanwe2014 on 16/7/15.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "GetHeadImgViewController.h"
  9. #import "AFNetworking.h"
  10. //#import "BGOssManager.h"
  11. #define kImgFrameDec 50
  12. #define kImgSize 8000 //头像大小
  13. @interface GetHeadImgViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate,MBProgressHUDDelegate>
  14. {
  15. CGFloat _headImgWH;
  16. NSData *_headImgData;
  17. CGSize _size;
  18. // BGOssManager *_ossManager;
  19. NSString *_uploadFilePath;
  20. NSString *_urlString;
  21. NSString *_timeString;//时间戳的字符串
  22. }
  23. @end
  24. @implementation GetHeadImgViewController
  25. -(UIImageView *)headImgView{
  26. if (!_headImgView) {
  27. _headImgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, kStatusBarHeight, kScreenW, kScreenW)];
  28. _headImgView.image = kDefaultPreloadHeadImg;
  29. // [self.headImgView sd_setImageWithURL:[NSURL URLWithString:self.headImgString] placeholderImage:kDefaultPreloadHeadImg];
  30. _headImgView.contentMode = UIViewContentModeScaleAspectFit;
  31. _headImgView.userInteractionEnabled = YES;
  32. }
  33. return _headImgView;
  34. }
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. }
  39. - (void)viewWillAppear:(BOOL)animated
  40. {
  41. [super viewWillAppear:animated];
  42. [self.navigationController setNavigationBarHidden:YES animated:YES];
  43. }
  44. -(void)viewWillDisappear:(BOOL)animated
  45. {
  46. [super viewWillDisappear:animated];
  47. [self.navigationController setNavigationBarHidden:NO animated:YES];
  48. }
  49. - (void)initFWUI
  50. {
  51. [super initFWUI];
  52. _urlString = @"";
  53. // if (self.BuguLive.appModel.open_sts == 1)
  54. // {
  55. // _ossManager = [[BGOssManager alloc]initWithDelegate:self];
  56. // }
  57. _headImgWH = 600;
  58. [self.view addSubview:self.headImgView];
  59. UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  60. backBtn.frame = CGRectMake(10, 10, 50, 50);
  61. [backBtn setImage:[UIImage imageNamed:@"com_arrow_vc_back_2"] forState:UIControlStateNormal];
  62. backBtn.layer.cornerRadius = 25;
  63. backBtn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
  64. [backBtn addTarget:self action:@selector(comeBack) forControlEvents:UIControlEventTouchUpInside];
  65. [self.headImgView addSubview:backBtn];
  66. self.photoButton.layer.cornerRadius = self.photoButton.frame.size.height/2;
  67. self.photoButton.backgroundColor = RGB(118, 59, 243);
  68. self.phoneChoseButton.layer.cornerRadius = self.phoneChoseButton.frame.size.height/2;
  69. self.phoneChoseButton.backgroundColor = RGB(118, 59, 243);
  70. [self.deleButton setTitleColor:kAppMainColor forState:0];
  71. }
  72. - (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
  73. {
  74. NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
  75. NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
  76. [imageData writeToFile:fullPath atomically:NO];
  77. _uploadFilePath = fullPath;
  78. NSLog(@"uploadFilePath : %@", _uploadFilePath);
  79. }
  80. #pragma mark 代理回调
  81. - (void)uploadImageWithUrlStr:(NSString *)imageUrlStr withUploadStateCount:(int)stateCount
  82. {
  83. [self hideMyHud];
  84. if (stateCount == 0)
  85. {
  86. _urlString = [NSString stringWithFormat:@"./%@",_timeString];
  87. [self updateHeadImage];
  88. }else
  89. {
  90. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"oss上传头像失败")];
  91. }
  92. }
  93. - (IBAction)buttonClick:(UIButton *)sender
  94. {
  95. int tag = (int) sender.tag;
  96. switch (tag)
  97. {
  98. case 0:
  99. [self getHeadImgViewTakePhone];
  100. break;
  101. case 1:
  102. [self getHeadImgViewFromLocal];
  103. break;
  104. case 2:
  105. [self comeBack];
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. - (void)getHeadImgViewTakePhone
  112. {
  113. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  114. picker.navigationBar.translucent=NO;
  115. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
  116. picker.sourceType=UIImagePickerControllerSourceTypeCamera;
  117. picker.delegate = self;
  118. picker.allowsEditing = YES;
  119. picker.navigationBar.translucent = NO;
  120. picker.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
  121. }
  122. [self presentViewController:picker animated:YES completion:nil];
  123. }
  124. - (void)getHeadImgViewFromLocal
  125. {
  126. UIImagePickerController *picker = [[UIImagePickerController alloc] init];
  127. picker.navigationBar.translucent=NO;
  128. if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
  129. {
  130. picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  131. picker.delegate = self;
  132. picker.allowsEditing = YES;
  133. picker.navigationBar.translucent = NO;
  134. // picker.modalPresentationStyle = UIModalTransitionStyleFlipHorizontal;
  135. }
  136. [self presentViewController:picker animated:YES completion:nil];
  137. }
  138. - (void)comeBack
  139. {
  140. [[AppDelegate sharedAppDelegate]popViewController];
  141. }
  142. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  143. {
  144. NSString *mediaType = [info objectForKey:UIImagePickerControllerEditedImage];
  145. if (mediaType)
  146. {
  147. UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
  148. if (!image)
  149. {
  150. image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  151. }
  152. if(image)
  153. {
  154. [self.headImgView setImage:image];
  155. }
  156. if (self.clickHeadImageBlock) {
  157. self.clickHeadImageBlock(image);
  158. }
  159. }
  160. [picker dismissViewControllerAnimated:YES completion:nil];
  161. [[AppDelegate sharedAppDelegate]popViewController];
  162. }
  163. - (void)getHeadNet
  164. {
  165. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  166. [parmDict setObject:@"login" forKey:@"ctl"];
  167. [parmDict setObject:@"do_update" forKey:@"act"];
  168. [parmDict setObject:self.userId forKey:@"id"];
  169. if (self.BuguLive.appModel.open_sts)
  170. {
  171. if (_urlString.length < 1 && [_urlString isEqualToString:@""])
  172. {
  173. [parmDict setObject:@"" forKey:@"oss_path"];
  174. }else
  175. {
  176. [parmDict setObject:_urlString forKey:@"oss_path"];
  177. }
  178. }
  179. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  180. {
  181. if ([responseJson toInt:@"status"] == 1)
  182. {
  183. [[BGHUDHelper sharedInstance] tipMessage:ASLocalizedString(@"头像更换成功")];
  184. }
  185. }FailureBlock:^(NSError *error)
  186. {
  187. }];
  188. }
  189. - (void)imageDidCut:(UIImage *)image
  190. {
  191. NSData *data=UIImageJPEGRepresentation(image, 0.00001);
  192. self.headImgView.image = [UIImage imageWithData:data];
  193. [self performSelector:@selector(uploadAvatar) withObject:nil afterDelay:0.8];
  194. }
  195. - (void)cyclicCompression:(UIImage *)image
  196. {
  197. _headImgWH -= kImgFrameDec;
  198. CGSize imagesize = image.size;
  199. imagesize.height = _headImgWH;
  200. imagesize.width = _headImgWH;
  201. UIImage *theImage = [self imageWithImageSimple:image scaledToSize:imagesize];
  202. _headImgData = UIImageJPEGRepresentation(theImage,0.00001);
  203. }
  204. //压缩图片尺寸
  205. - (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
  206. {
  207. UIGraphicsBeginImageContext(newSize);
  208. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  209. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  210. UIGraphicsEndImageContext();
  211. return newImage;
  212. }
  213. #pragma mark 使用流文件上传头像
  214. - (void)uploadAvatar
  215. {
  216. [self showMyHud];
  217. NSString *imageFile = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
  218. NSString *photoName=[imageFile stringByAppendingPathComponent:@"image_head.jpg"];
  219. NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"file:%@",photoName]];
  220. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  221. [parmDict setObject:@"avatar" forKey:@"ctl"];
  222. [parmDict setObject:@"uploadImage" forKey:@"act"];
  223. if (self.userId)
  224. {
  225. [parmDict setObject:self.userId forKey:@"id"];
  226. }
  227. FWWeakify(self)
  228. [self.httpsManager POSTWithDict:parmDict andFileUrl:fileUrl SuccessBlock:^(NSDictionary *responseJson)
  229. {
  230. FWStrongify(self)
  231. if ([responseJson toInt:@"status"] == 1)
  232. {
  233. _urlString = [responseJson toString:@"path"];
  234. [self hideMyHud];
  235. [self updateHeadImage];
  236. }else
  237. {
  238. [BGHUDHelper alert:[responseJson toString:@"error"]];
  239. }
  240. } FailureBlock:^(NSError *error)
  241. {
  242. [self hideMyHud];
  243. }];
  244. }
  245. - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  246. {
  247. [self dismissViewControllerAnimated:YES completion:^{}];
  248. }
  249. - (void)updateHeadImage
  250. {
  251. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  252. [parmDict setObject:@"login" forKey:@"ctl"];
  253. [parmDict setObject:@"do_update" forKey:@"act"];
  254. [parmDict setObject:self.userId forKey:@"id"];
  255. if (_urlString.length < 1 && [_urlString isEqualToString:@""])
  256. {
  257. [parmDict setObject:@"" forKey:@"normal_head_path"];
  258. }else
  259. {
  260. [parmDict setObject:_urlString forKey:@"normal_head_path"];
  261. }
  262. [parmDict setObject:@"1" forKey:@"type"];
  263. [self showMyHud];
  264. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  265. {
  266. [self hideMyHud];
  267. } FailureBlock:^(NSError *error)
  268. {
  269. [self hideMyHud];
  270. }];
  271. }
  272. @end