123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445 |
- #import "PostTopicVC.h"
- #import "UzysAssetsPickerController.h"
- #import "LDPostVCbtnView.h"
- #define defTitle @"选择分组"
- @interface PostTopicVC ()<UzysAssetsPickerControllerDelegate>
- {
- /**输入框
- */
- UITextView* textVi;
-
- /**标签按钮
- */
- UIButton* tagBtn;
-
-
- UIButton* currentBtn;
-
- /**多个title的父视图
- */
- UIView* contentV;
-
- /**从照片库挑选的照片
- */
- UIImage* pickedImg;
- /**从照片库挑选的所有照片
- */
- NSMutableArray *imageArray;
- /**存放之前的imageView
- */
- NSMutableArray *imageViews;
- /**添加图片按钮。
- */
- UIButton* addImgBtn;
- }
- @property(nonatomic,strong)NSMutableArray* titles;
- @end
- @implementation PostTopicVC
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self myInit];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- -(void)viewDidAppear:(BOOL)animated
- {
- [super viewDidAppear:animated];
- }
- #pragma mark -
- -(void)myInit
- {
- [self configNavigationBar];
- [self setTitle:@"编辑"];
- [self.view setBackgroundColor:[UIColor whiteColor]];
- self.navigationController.navigationBar.translucent = NO;
-
- imageArray = [NSMutableArray array];
- imageViews = [NSMutableArray array];
- //最好是。它们的tag值不要变。
- NSArray* arr;
-
- arr = @[@"吐槽",@"晒图",@"找驾校",@"科目一",@"科目二",@"科目三",@"科目四",@"下证",@"许愿"];
- if (_groupId.intValue == -1)
- {
- if (_jxjc.length == 0 && defUser.userSchoolName)
- {
- _jxjc = defUser.userSchoolName;
- arr = @[_jxjc,@"吐槽",@"晒图",@"找驾校",@"科目一",@"科目二",@"科目三",@"科目四",@"下证",@"许愿"];
- }
-
- }
- _titles = [NSMutableArray arrayWithArray:arr];
-
-
- CGFloat x,y,w,h,bd;
-
- NSInteger tag = 0;
- UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 60, 30)];
- [btn setBackgroundColor:defGreen];
- [btn setTitle:@"发布" forState:UIControlStateNormal];
- UIBarButtonItem* bbi = [[UIBarButtonItem alloc] initWithCustomView:btn];
- self.navigationItem.rightBarButtonItem = bbi;
- [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- [btn setTag:tag++];
-
- bd = 10;
- x = 0;
- y = 0;
- w = kSize.width - bd;
- h = 100;
- UITextView* tv = [[UITextView alloc] initWithFrame:CGRectMake(x, y, w, h)];
- [self.view addSubview:tv];
- textVi = tv;
- [tv setFont:[UIFont scaleSize:NormalFont]];
-
- arr = @[@"",@"心情像朝日的阳光,连学生都 变成了开心的事",@"振作心情! 驾照就在不远处",
- @"普普通通的一天,普普通通的心情",@"道理我都懂,可是为什么考驾 照这么烦。",
- @"不懂啊,完全不懂啊",@"啊!为什么我要学车!!",@"",@"",@""];
- if (3 == _type.intValue) {
- [tv setText:arr[_mood]];
- }
-
- x = bd;
- y += h+bd;
- w = h = (kSize.width - 5*bd)/4;
- btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
- [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- [btn borderColor:KlineColor width:1 cornorRadios:3];
- [self.view addSubview:btn];
- [btn setTag:tag++];
- addImgBtn = btn;
-
- NSArray *images;
- images = @[@"",@"mood0.png",@"mood1.png",@"mood2.png",@"mood3.png",@"mood4.png",@"mood5.png"];
- if (3 == _type.intValue) {
- pickedImg = [UIImage imageNamed:images[_mood]];
- [btn setImage:pickedImg forState:UIControlStateNormal];
- }else{
- [btn setTitle:@"十" textColor:KlineColor font:35 fotState:UIControlStateNormal];
- }
-
- y+= h + 25;
- h = 30;
- w = [defTitle sizeForFont:13].width + 20;
- btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
- [btn setTitle:defTitle textColor:contentTextColor font:13 fotState:UIControlStateNormal];
- [btn borderColor:KlineColor width:1 cornorRadios:h/2.0];
- [btn setTitleColor:defGreen forState:UIControlStateSelected];
- [self.view addSubview:btn];
- [btn setTag:tag++];
- tagBtn = btn;
-
- arr = @[@"holder",defTitle,@"学车记录",@"学车直播",@"学车问答"];
- //如果groupId为0就会target。否则。固定。
- if ( 1 == _type.intValue)
- {
- if(!_groupId || [_groupId isEqualToString:@"0"])
- {
- [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- }
- else
- {
- //如果是驾校 就传个-1过去
- if ([_groupId isEqualToString:@"-1"]) {
- [btn setTitleNormal:_titles[0]];
- }else{
- [btn setTitleNormal:_titles[_groupId.intValue-1]];
- }
-
- btn.width = [arr[_type.intValue] sizeForFont:13].width + 20;
- [btn setSelected:YES];
- [btn borderColor:defGreen width:1 cornorRadios:h/2.0];
- }
- }
- else
- {
- [btn setTitleNormal:arr[_type.intValue]];
- btn.width = [arr[_type.intValue] sizeForFont:13].width + 20;
- [btn setSelected:YES];
- [btn borderColor:defGreen width:1 cornorRadios:h/2.0];
- }
-
- //tag 3+
- x = 0;
- y+= h+ 30;
- w = kSize.width;
- h = kSize.height - kNavOffSet - y;
- UIView* vi = [[UIView alloc] initWithFrame:CGRectMake(x, y, w, h)];
- [vi setBackgroundColor:backGroundColor];
- [self.view addSubview:vi];
- contentV = vi;
- contentV.hidden = YES;
-
- h = 30;
- y = x = bd;
- for (int i= 0; i<_titles.count ; i++) {
- NSString* str = _titles[i];
- w = [str sizeForFont:13].width + 20;
- if (x + w + bd > kSize.width) {
- x = bd;
- y += bd + h;
- }
-
- btn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
- [btn setTitle:str textColor:contentTextColor font:13 fotState:UIControlStateNormal];
- [btn setTitle:str textColor:defGreen font:13 fotState:UIControlStateSelected];
- [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
- [btn borderColor:KlineColor width:1 cornorRadios:h/2.0];
- [contentV addSubview:btn];
- [btn setTag:tag++];
- x += w+bd;
- }
- }
- -(void)btnClick:(UIButton*)sender
- {
- [self.view endEditing:YES];
- switch (sender.tag) {
- case 0:
- //发布话题
- [self uploadTopicInfo];
- break;
- case 1:
- //选择图片
- [self upDatePhotos];
- break;
- case 2:
- //选择话题分组
- [contentV setHidden:NO];
- break;
- default:
- {
- CGFloat h = 30;
- NSString* str ;
- CGFloat w ;
- if (sender != currentBtn && currentBtn) {
- currentBtn.selected = NO;
- [currentBtn borderColor:KlineColor width:1 cornorRadios: h/2.0];
- }
- sender.selected = !sender.selected;
- tagBtn.selected = sender.selected;
- currentBtn = sender;
- if (sender.selected) {
- [sender borderColor:defGreen width:1 cornorRadios: h/2.0];
- [tagBtn borderColor:defGreen width:1 cornorRadios: h/2.0];
- [tagBtn setTitle: [sender titleForState:UIControlStateNormal] forState:UIControlStateNormal];
-
- str = [sender titleForState:UIControlStateNormal];
- w = [str sizeForFont:13].width + 20;
- }else{
- [sender borderColor:KlineColor width:1 cornorRadios: h/2.0];
- [tagBtn borderColor:KlineColor width:1 cornorRadios: h/2.0];
- str = defTitle;
- w = [str sizeForFont:13].width + 20;
- [tagBtn setTitle:defTitle forState:UIControlStateNormal];
- }
- tagBtn.width = w;
- }
- break;
- }
- }
- #pragma mark 调用系统相册
- -(void)upDatePhotos
- {
- [imageArray removeAllObjects];
- UzysAssetsPickerController *picker = [[UzysAssetsPickerController alloc] init];
- picker.delegate = self;
- picker.maximumNumberOfSelectionVideo = 0;
- picker.maximumNumberOfSelectionPhoto = 9;
- picker.modalPresentationStyle = UIModalPresentationFullScreen;
- [self presentViewController:picker animated:YES completion:nil];
- }
- - (void)UzysAssetsPickerController:(UzysAssetsPickerController *)picker didFinishPickingAssets:(NSArray *)assets
- {
- for (ALAsset *representation in assets)
- {
- UIImage *img = [UIImage imageWithCGImage:representation.defaultRepresentation.fullResolutionImage
- scale:representation.defaultRepresentation.scale
- orientation:(UIImageOrientation)representation.defaultRepresentation.orientation];
- if (img == nil) {
- continue;
- }
- [imageArray addObject:img];
- }
-
- [self setUI];
- }
- -(void)setUI
- {
- CGFloat marginX = 10;
- CGFloat width = addImgBtn.width;
- if (imageViews.count != 0)
- {
- addImgBtn.frame = CGRectMake(marginX, textVi.y + textVi.height + marginX, width, width);
- for (UIImageView *image in imageViews)
- {
- [image removeFromSuperview];
- }
- }
- CGFloat marginY = addImgBtn.y;
- for (int i = 0; i < imageArray.count + 1; i ++)
- {
- int row = i/4;
- int column = i%4;
- LDPostVCbtnView *image = [[LDPostVCbtnView alloc] initWithFrame:CGRectMake(marginX + column*(marginX + width), marginY + row*(marginX + width), width, width)];
- if (i == imageArray.count)
- {
- addImgBtn.frame = image.frame;
- break;
- }
- image.image = imageArray[i];
- image.userInteractionEnabled = YES;
- [self.view addSubview:image];
- [imageViews addObject:image];
-
- //btn位置及图片这么做之后 用户体验绝对爽的一比
- UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(width - 20, -20, 40, 40)];
- [btn setImage:[UIImage imageNamed:@"community6"] forState:UIControlStateNormal];
- [btn setImageEdgeInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
- btn.tag = 50 + i;
- [btn addTarget:self action:@selector(deleteImage:) forControlEvents:UIControlEventTouchUpInside];
- [image addSubview:btn];
- }
-
- //改变UI
- CGRect rect;
- CGFloat newY = addImgBtn.y + addImgBtn.height + 25;
-
- rect = tagBtn.frame;
- rect.origin.y = newY;
- tagBtn.frame = rect;
-
- rect = contentV.frame;
- rect.origin.y = newY + tagBtn.height + 30;
- contentV.frame = rect;
- }
- -(void)deleteImage:(UIButton *)btn
- {
- [imageArray removeObjectAtIndex:(btn.tag - 50)];
- [self setUI];
- }
- -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
- {
- if (textVi.isFirstResponder) {
- [textVi resignFirstResponder];
- }
- [contentV setHidden:YES];
- }
- -(void)complete:(void (^)(void))comp
- {
- fresh = comp;
- }
- #pragma mark 发布话题
- /**没有驾校就是9个。有是10个。
- */
- -(void)uploadTopicInfo
- {
- if (textVi.text.length < 6) {
- ShowMsg(@"最少输入6个字");
- return;
- }
- //过滤字 500以内
- if (![Util connectedToNetWork]) {
- showMsgUnconnect();
- return;
- }
-
- if (imageArray.count == 0) {
- //无图片上传
- [self uploadTopicInfoWithImageURL:@""];
- }else{
- //有图片上传
- NSMutableArray *imgDataArray = [NSMutableArray arrayWithCapacity:imageArray.count];
-
- NSData *imgData;
- for (UIImage *updataImg in imageArray) {
- //这个比例似乎有问题 但只能先这样了 完善之图片清晰度--danson
- imgData = UIImageJPEGRepresentation(updataImg,0.5);
- //imgData = UIImageJPEGRepresentation([updataImg scaledToSize:CGSizeMake(kFrame.size.width, kFrame.size.height)],0.5);
- //NSData *data = UIImagePNGRepresentation([updataImg scaledToSize:CGSizeMake(230, 230)]);
- //NSString *string = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
- [imgDataArray addObject:imgData];
- }
-
- NSString *method = @"topicImg";
- [MBProgressHUD showLoadToView:self.view];
- [jiaPeiManager requestAnythingWithURL:method array:imgDataArray data:imgData completion:^(NSDictionary *root) {
-
- [MBProgressHUD hideHUDForView:self.view];
- if (!root) {
- ShowMsg(@"上传失败");
- return;
- }
- if (![root[@"code"] isEqualToString:@"0"]) {
- ShowMsg(root[@"body"]);
- return;
- }
-
- [self uploadTopicInfoWithImageURL:root[@"body"]];
- }];
- }
- }
- -(void)uploadTopicInfoWithImageURL:(NSString *)imgUrl
- {
- if (!_type) {
- _type = @"1";
- }
- if(!_groupId){
- if (currentBtn && currentBtn.selected ) {
- _groupId = [NSString stringWithFormat:@"%d",(int)currentBtn.tag-2];
- }else{
- _groupId = @"1";
- }
- }
-
- NSMutableArray *arr=[NSMutableArray array];
- [arr addPro:@"user" Value:defUser.userDict[@"id"]];
- [arr addPro:@"content" Value:textVi.text];
- [arr addPro:@"pictures" Value:imgUrl];
- [arr addPro:@"groupId" Value:_groupId];
- [arr addPro:@"score" Value:@"80"];
- [arr addPro:@"type" Value:_type];
- [arr addPro:@"moodId" Value:[NSString stringWithFormat:@"%d",(int)_mood]];
- [arr addPro:@"childGroupId" Value:_childID];
-
- NSString* method = @"uploadTopicInfo";
- [MBProgressHUD showLoadToView:self.view];
- [jiaPeiManager requestAnythingWithURL:method array:arr data:nil completion:^(NSDictionary * dict) {
- [MBProgressHUD hideHUDForView:self.view];
- if (!dict) {
- ShowMsgFailed();
- return;
- }
- if ( [dict[@"code"] isEqualToString:@"1"]) {
- ShowMsg(dict[@"body"]);
- return;
- }
-
- ShowMsgSuc();
- if (fresh) {
- fresh();
- }
- [self.navigationController popViewControllerAnimated:YES];
- }];
- }
- @end
|