JXSetChatBackgroundVC.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. //
  2. // JXSetChatBackgroundVC.m
  3. // shiku_im
  4. //
  5. // Created by p on 2017/12/8.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXSetChatBackgroundVC.h"
  9. #import "JXCameraVC.h"
  10. #define HEIGHT 56
  11. @interface JXSetChatBackgroundVC ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,JXCameraVCDelegate>
  12. @end
  13. @implementation JXSetChatBackgroundVC
  14. - (instancetype)init {
  15. if ([super init]) {
  16. }
  17. return self;
  18. }
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. self.isGotoBack = YES;
  22. self.title = Localized(@"JX_SettingUpChatBackground");
  23. self.heightHeader = JX_SCREEN_TOP;
  24. self.heightFooter = 0;
  25. //self.view.frame = CGRectMake(0, 0, JX_SCREEN_WIDTH, JX_SCREEN_HEIGHT);
  26. [self createHeadAndFoot];
  27. self.tableBody.backgroundColor = HEXCOLOR(0xF2F2F2);
  28. self.tableBody.scrollEnabled = YES;
  29. int h=0;
  30. int w=JX_SCREEN_WIDTH;
  31. JXImageView* iv;
  32. iv = [self createButton:Localized(@"JX_SelectionFromHandsetAlbum") drawTop:NO drawBottom:YES icon:nil click:@selector(onPickPhoto)];
  33. iv.frame = CGRectMake(0,h, w, HEIGHT);
  34. h+=iv.frame.size.height;
  35. iv = [self createButton:Localized(@"JX_TakeAPicture") drawTop:NO drawBottom:NO icon:nil click:@selector(onCamera)];
  36. iv.frame = CGRectMake(0,h, w, HEIGHT);
  37. h+=iv.frame.size.height + 8;
  38. iv = [self createButton:Localized(@"JX_RestoreDefaultBackground") drawTop:NO drawBottom:NO icon:nil click:@selector(onDefault)];
  39. iv.frame = CGRectMake(0,h, w, HEIGHT);
  40. }
  41. // 从手机相册选择
  42. - (void)onPickPhoto {
  43. UIImagePickerController *imgPicker=[[UIImagePickerController alloc]init];
  44. [imgPicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
  45. [imgPicker setDelegate:self];
  46. [imgPicker setAllowsEditing:NO];
  47. [self presentViewController:imgPicker animated:YES completion:^{}];
  48. }
  49. // 拍照
  50. - (void)onCamera {
  51. JXCameraVC *vc = [JXCameraVC alloc];
  52. vc.cameraDelegate = self;
  53. vc.isPhoto = YES;
  54. vc = [vc init];
  55. [self presentViewController:vc animated:YES completion:nil];
  56. }
  57. // 恢复默认
  58. - (void)onDefault {
  59. if (self.userId.length > 0) {
  60. [g_constant.userBackGroundImage removeObjectForKey:self.userId];
  61. BOOL isSuccess = [g_constant.userBackGroundImage writeToFile:backImage atomically:YES];
  62. [g_notify postNotificationName:kSetBackGroundImageView object:nil];
  63. if (isSuccess) {
  64. [g_App showAlert:Localized(@"JX_SetUpSuccess")];
  65. }else {
  66. [g_App showAlert:Localized(@"JX_SettingFailure")];
  67. }
  68. return;
  69. }
  70. if(![[NSFileManager defaultManager] fileExistsAtPath:kChatBackgroundImagePath]) {
  71. [g_App showAlert:Localized(@"JX_SetUpSuccess")];
  72. return;
  73. }
  74. NSError *error = nil;
  75. [[NSFileManager defaultManager] removeItemAtPath:kChatBackgroundImagePath error:&error];
  76. if (!error) {
  77. [g_App showAlert:Localized(@"JX_SetUpSuccess")];
  78. }else {
  79. [g_App showAlert:Localized(@"JX_SettingFailure")];
  80. }
  81. }
  82. #pragma mark ----------图片选择完成-------------
  83. //UIImagePickerController代理方法
  84. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  85. {
  86. [self dismissViewControllerAnimated:YES completion:NULL];
  87. UIImage * chosedImage=[info objectForKey:@"UIImagePickerControllerOriginalImage"];
  88. NSData *imageData = UIImageJPEGRepresentation(chosedImage, 1);
  89. BOOL isSuccess = NO;
  90. if (self.userId.length > 0) {
  91. // if ([self.delegate respondsToSelector:@selector(setChatBackgroundVC:image:)]) {
  92. // [self.delegate setChatBackgroundVC:self image:chosedImage];
  93. // }
  94. [g_constant.userBackGroundImage setObject:imageData forKey:self.userId];
  95. isSuccess = [g_constant.userBackGroundImage writeToFile:backImage atomically:YES];
  96. [g_notify postNotificationName:kSetBackGroundImageView object:chosedImage];
  97. }else {
  98. isSuccess = [imageData writeToFile:kChatBackgroundImagePath atomically:YES];
  99. }
  100. if (isSuccess) {
  101. [g_App showAlert:Localized(@"JX_SetUpSuccess")];
  102. }else {
  103. [g_App showAlert:Localized(@"JX_SettingFailure")];
  104. }
  105. }
  106. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
  107. {
  108. [self dismissViewControllerAnimated:NO completion:^{
  109. }];
  110. }
  111. // 拍照
  112. - (void)cameraVC:(JXCameraVC *)vc didFinishWithImage:(UIImage *)image {
  113. NSData *imageData = UIImageJPEGRepresentation(image, 1);
  114. BOOL isSuccess = NO;
  115. if (self.userId.length > 0) {
  116. // if ([self.delegate respondsToSelector:@selector(setChatBackgroundVC:image:)]) {
  117. // [self.delegate setChatBackgroundVC:self image:image];
  118. // }
  119. [g_constant.userBackGroundImage setObject:imageData forKey:self.userId];
  120. isSuccess = [g_constant.userBackGroundImage writeToFile:backImage atomically:YES];
  121. [g_notify postNotificationName:kSetBackGroundImageView object:image];
  122. }else {
  123. isSuccess = [imageData writeToFile:kChatBackgroundImagePath atomically:YES];
  124. }
  125. if (isSuccess) {
  126. [g_App showAlert:Localized(@"JX_SetUpSuccess")];
  127. }else {
  128. [g_App showAlert:Localized(@"JX_SettingFailure")];
  129. }
  130. }
  131. -(JXImageView*)createButton:(NSString*)title drawTop:(BOOL)drawTop drawBottom:(BOOL)drawBottom icon:(NSString*)icon click:(SEL)click{
  132. JXImageView* btn = [[JXImageView alloc] init];
  133. btn.backgroundColor = [UIColor whiteColor];
  134. btn.userInteractionEnabled = YES;
  135. btn.didTouch = click;
  136. btn.delegate = self;
  137. [self.tableBody addSubview:btn];
  138. JXLabel* p = [[JXLabel alloc] initWithFrame:CGRectMake(15, 0, JX_SCREEN_WIDTH-100, HEIGHT)];
  139. p.text = title;
  140. p.font = g_factory.font16;
  141. p.backgroundColor = [UIColor clearColor];
  142. p.textColor = [UIColor blackColor];
  143. p.delegate = self;
  144. p.didTouch = click;
  145. [btn addSubview:p];
  146. if(icon){
  147. UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(10, (HEIGHT-20)/2, 20, 20)];
  148. iv.image = [UIImage imageNamed:icon];
  149. [btn addSubview:iv];
  150. }
  151. if(drawTop){
  152. UIView* line = [[UIView alloc] initWithFrame:CGRectMake(15,0,JX_SCREEN_WIDTH-15,LINE_WH)];
  153. line.backgroundColor = THE_LINE_COLOR;
  154. [btn addSubview:line];
  155. }
  156. if(drawBottom){
  157. UIView* line = [[UIView alloc] initWithFrame:CGRectMake(15,HEIGHT-LINE_WH,JX_SCREEN_WIDTH-15,LINE_WH)];
  158. line.backgroundColor = THE_LINE_COLOR;
  159. [btn addSubview:line];
  160. }
  161. if(click){
  162. UIImageView* iv;
  163. iv = [[UIImageView alloc] initWithFrame:CGRectMake(JX_SCREEN_WIDTH-15-7, (HEIGHT-13)/2, 7, 13)];
  164. iv.image = [UIImage imageNamed:@"new_icon_>"];
  165. [btn addSubview:iv];
  166. }
  167. return btn;
  168. }
  169. - (void)didReceiveMemoryWarning {
  170. [super didReceiveMemoryWarning];
  171. // Dispose of any resources that can be recreated.
  172. }
  173. /*
  174. #pragma mark - Navigation
  175. // In a storyboard-based application, you will often want to do a little preparation before navigation
  176. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  177. // Get the new view controller using [segue destinationViewController].
  178. // Pass the selected object to the new view controller.
  179. }
  180. */
  181. @end