123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432 |
- //
- // SocialPublishViewController.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/6/20.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "SocialPublishViewController.h"
- #import "UIView+NTES.h"
- #import "UIView+Toast.h"
- #import "User.h"
- #import "NIMKitMediaFetcher.h"
- #import "SVProgressHUD.h"
- #import "UIImage+NIM.h"
- #import "AFNetworking/AFNetworking.h"
- #import "LocationManager.h"
- #import "HttpRequest.h"
- @interface SocialPublishViewController ()<UITextViewDelegate, LocationDelegate>
- @property (nonatomic, strong) LocationManager *locationManager;
- @property (nonatomic, assign) double longitude;
- @property (nonatomic, assign) double latitude;
- @property (nonatomic, strong) UITextView *msgTxtView;
- @property (nonatomic, strong) UILabel *msgReplaceHolder;
- @property (nonatomic, strong) UILabel *locationLabel;
- @property (nonatomic, strong) UILabel *picLabel;
- @property (nonatomic, strong) NSMutableArray *picArr;
- @property (nonatomic, strong) NSMutableArray *picDataArr;
- @property (nonatomic, assign) int picUploadIndex;
- @property (nonatomic, strong) UIView *picContainer;
- @property (nonatomic, strong) UIButton *picAddBtn;
- @property (nonatomic, strong) NIMKitMediaFetcher *mediaFetcher;
- @end
- int maxCountPictures = 9;
- CGFloat picPadding = 5;
- int picColumns = 5;
- @implementation SocialPublishViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- self.view.backgroundColor = UIColor.whiteColor;
-
- _picArr = [[NSMutableArray alloc] init];
- _picDataArr = [[NSMutableArray alloc] init];
-
-
-
-
- UINavigationItem *navItem = self.navigationItem;
- [navItem setTitle:@"发布"];
-
- CGFloat navBottom = self.navigationController.navigationBar.bottom;
-
- _msgTxtView = [[UITextView alloc] initWithFrame:CGRectMake(15, navBottom + 10, self.view.width-30, 100)];
- [_msgTxtView setText:@""];
- _msgTxtView.delegate = self;
- _msgTxtView.font = [UIFont systemFontOfSize:15];
- _msgTxtView.backgroundColor = UIColor.clearColor;
- _msgReplaceHolder = [[UILabel alloc] initWithFrame:CGRectMake(15, navBottom + 10, self.view.width-30, 30)];
- _msgReplaceHolder.textColor = UIColor.lightGrayColor;
- _msgReplaceHolder.text = @"你想说点什么";
- _msgReplaceHolder.font = [UIFont systemFontOfSize:15];
-
- [self.view addSubview:_msgReplaceHolder];
- [self.view addSubview:_msgTxtView];
-
-
-
-
-
-
- UIView *locationView = [[UIView alloc] initWithFrame:CGRectMake(15, _msgTxtView.bottom + 10, 100, 30)];
- locationView.layer.cornerRadius = 15;
- locationView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
-
- UIImageView *locationIcon = [[UIImageView alloc] initWithFrame:CGRectMake(10, 4, 18, 22)];
- [locationIcon setImage:[UIImage imageNamed:@"朋友圈发布_03"]];
- [locationView addSubview:locationIcon];
-
- _locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(locationIcon.right+5, 0, 0, 30)];
- [_locationLabel setText:[User sharedInfo].district ? [NSString stringWithFormat:@"%@%@", [User sharedInfo].city, [User sharedInfo].district] : @""];
- _locationLabel.textColor = UIColor.grayColor;
- _locationLabel.font = [UIFont systemFontOfSize:15];
- [_locationLabel sizeToFit];
- _locationLabel.top = (30 - _locationLabel.height)/2;
- [locationView addSubview:_locationLabel];
-
- locationView.width = _locationLabel.right + 10;
- [self.view addSubview:locationView];
-
-
-
-
-
-
-
- _picLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, locationView.bottom + 10, 150, 30)];
- _picLabel.textColor = UIColor.lightGrayColor;
- [self.view addSubview:_picLabel];
- [self updatePictureTip];
-
-
- float picSize = (self.view.width - 20 - picPadding * (picColumns - 1)) / picColumns;
- _picContainer = [[UIView alloc] initWithFrame:CGRectMake(10, _picLabel.bottom+10, self.view.width - 20, picSize * 2 + picPadding)];
- [self.view addSubview:_picContainer];
-
- _picAddBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, picSize, picSize)];
- [_picAddBtn setImage:[UIImage imageNamed:@"朋友圈发布_07"] forState:UIControlStateNormal];
- [_picAddBtn addTarget:self action:@selector(onTouchAddPic:) forControlEvents:UIControlEventTouchUpInside];
- [_picContainer addSubview:_picAddBtn];
-
-
-
- UIButton *publishBtn = [[UIButton alloc] initWithFrame:CGRectMake(15, _picContainer.bottom+20, self.view.width - 30, 50)];
- [publishBtn setTitle:@"发布" forState:UIControlStateNormal];
- [publishBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
- publishBtn.backgroundColor = User.orangeColor;
- publishBtn.layer.cornerRadius = 5;
- [publishBtn addTarget:self action:@selector(onTouchPublish:) forControlEvents:UIControlEventTouchUpInside];
- [self.view addSubview:publishBtn];
-
-
-
- _locationManager = [LocationManager sharedManager];
- _locationManager.delegate = self;
- [_locationManager startUpdate];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
-
- [_locationManager stopUpdate];
- _locationManager.delegate = nil;
-
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- }
- - (void)coordinateUpdate:(double)longitude latitude:(double)latitude{
-
- self.longitude = longitude;
- self.latitude = latitude;
-
- __weak typeof(self) wself = self;
- [[HttpRequest shared] location:longitude latitude:latitude success:^(NSString * _Nullable province, NSString * _Nullable city, NSString * _Nullable district, NSString * _Nullable provinceRoomId, NSString * _Nullable cityRoomId, NSString * _Nullable districtRoomId) {
-
- if(city && district && city.length > 0 && district.length > 0)
- {
- if([city isEqualToString:district])
- {
- wself.locationLabel.text = district;
- }
- else
- {
- wself.locationLabel.text = [NSString stringWithFormat:@"%@%@", city, district];
- }
- [wself.locationManager stopUpdate];
- }
-
- } failure:^{
-
- }];
- }
- - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
- {
- if (![text isEqualToString:@""])
- {
- _msgReplaceHolder.hidden = YES;
- }
- if ([text isEqualToString:@""] && range.location == 0 && range.length == 1)
- {
- _msgReplaceHolder.hidden = NO;
- }
- return YES;
- }
- - (void)updatePictureTip
- {
- _picLabel.text = [NSString stringWithFormat:@"照片不超过%d张", (int)(maxCountPictures - _picArr.count)];
-
- }
- - (void)layoutPictures
- {
- float picSize = (self.view.width - 20 - picPadding * (picColumns - 1)) / picColumns;
-
- CGFloat startX = 0;
- CGFloat startY = 0;
-
- int count = (int)_picArr.count;
- int i = 0;
- for(i=0; i<count; i++){
- startX = (i % picColumns) * (picSize + picPadding);
- startY = floorf(i / picColumns);
-
- UIImageView *imgView = nil;
- if(_picContainer.subviews.count - 1 <= i){
- imgView = [[UIImageView alloc] initWithFrame:CGRectMake(startX, startY, picSize, picSize)];
- [_picContainer addSubview:imgView];
- }
- else{
- imgView = [_picContainer.subviews objectAtIndex:i];
- imgView.left = startX;
- imgView.top = startY;
- }
-
- UIImage *img = [_picArr objectAtIndex:i];
- [imgView setImage:img];
- }
-
- if(count < maxCountPictures)
- {
- startX = (count % picColumns) * (picSize + picPadding);
- startY = floorf(count / picColumns);
- _picAddBtn.left = startX;
- _picAddBtn.top = startY;
- _picAddBtn.hidden = NO;
- }
- else
- {
- _picAddBtn.hidden = YES;
- }
- }
- - (void)onTouchAddPic:(int)sender
- {
- [self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
- }
- - (NIMKitMediaFetcher *)mediaFetcher
- {
- if (!_mediaFetcher) {
- _mediaFetcher = [[NIMKitMediaFetcher alloc] init];
- }
- return _mediaFetcher;
- }
- - (void)showImagePicker:(UIImagePickerControllerSourceType)type{
-
- __weak typeof(self) weakSelf = self;
- [self.mediaFetcher fetchPhotoFromLibrary:^(NSArray *images, NSString *path, PHAssetMediaType type) {
-
- int a = 1;
- NSLog(@"%d", a);
-
- switch (type) {
- case PHAssetMediaTypeImage:
- {
- for (UIImage *image in images) {
- [weakSelf.picArr addObject:image];
- }
- [weakSelf layoutPictures];
- }
- break;
- case PHAssetMediaTypeVideo:
- {
- }
- break;
- default:
- return;
- }
-
- }];
- }
- - (void)onTouchPublish:(int)sender{
- [SVProgressHUD show];
- [self uploadPicture];
- }
-
- - (void)uploadPicture{
-
- if(_msgTxtView.text.length == 0 && _picArr.count == 0)
- {
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"你还什么都没说呢" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
- [alert show];
- return;
- }
-
- if(_picUploadIndex >= _picArr.count)
- {
- [self publish];
- return;
- }
-
- UIImage *image = [_picArr objectAtIndex:_picUploadIndex];
- NSString *userId = [NSString stringWithFormat:@"%d", [User sharedInfo].userId];
- NSDictionary *dict = @{@"user_id":userId};
-
- NSString *urlString = @"http://whosay.dashgame.com/index.php?m=who&c=social&a=upload";
-
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.responseSerializer = [AFJSONResponseSerializer serializer];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/html", nil];
-
- [manager POST:urlString parameters:dict constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
- NSData *data = UIImageJPEGRepresentation(image, 1);
- [formData appendPartWithFileData:data name:@"upload" fileName:[NSString stringWithFormat:@"image_%d.jpg", _picUploadIndex] mimeType:@"image/jpg"];
-
- } progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
-
- //NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
- //NSLog(@"请求成功,服务器返回的信息%@", string);
-
- NSLog(@"请求成功,服务器返回的信息%@", responseObject);
-
- int code = [[responseObject objectForKey:@"c"] intValue];
- if(code == 0)
- {
- NSDictionary *data = [responseObject objectForKey:@"d"];
- NSString *aid = [data objectForKey:@"aid"];
-
- if(_picUploadIndex >= _picDataArr.count)
- {
- [_picDataArr addObject:aid];
- }
- else
- {
- [_picDataArr setObject:aid atIndexedSubscript:_picUploadIndex];
- }
-
- _picUploadIndex++;
- [self uploadPicture];
- }
- else{
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[responseObject objectForKey:@"d"] delegate:self cancelButtonTitle:@"放弃" otherButtonTitles:@"重试", nil];
- [alert show];
- }
- } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
- NSLog(@"请求失败,服务器返回的错误信息%@",error);
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"上传图片失败" delegate:self cancelButtonTitle:@"放弃" otherButtonTitles:@"重试", nil];
- [alert show];
- }];
- }
-
- - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
- switch (buttonIndex) {
- case 0:
- [self.navigationController popViewControllerAnimated:YES];
- break;
- case 1:
- [self uploadPicture];
- break;
- default:
- break;
- }
- }
- - (void)publish
- {
- User *user = [User sharedInfo];
-
- NSString *userId = [NSString stringWithFormat:@"%d", user.userId];
-
- // NSMutableDictionary *parameters = @{@"id":userId,
- // @"local":_locationLabel.text,
- // @"msg":_msgTxtView.text,
- // @"pic":_picDataArr,
- // };
- NSString *local = _locationLabel.text ? _locationLabel.text : @"";
- NSString *msg = _msgTxtView.text ? _msgTxtView.text : @"";
-
-
- NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
- [parameters setObject:userId forKey:@"id"];
- [parameters setObject:local forKey:@"local"];
- [parameters setObject:msg forKey:@"msg"];
- [parameters setObject:_picDataArr forKey:@"pic"];
- [parameters setObject:[NSString stringWithFormat:@"%F", _longitude] forKey:@"longitude"];
- [parameters setObject:[NSString stringWithFormat:@"%F", _latitude] forKey:@"latitude"];
-
- NSString *urlString = @"http://whosay.dashgame.com/index.php?m=who&c=social&a=publish";
- //请求的managers
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.responseSerializer = [AFJSONResponseSerializer new];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
-
- //请求的方式:POST
-
- NSLog(@"%@", parameters);
-
- [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
-
- [SVProgressHUD dismiss];
-
- NSLog(@"请求成功,服务器返回的信息%@", responseObject);
- NSDictionary * dic = responseObject;
- int code = [[dic valueForKey:@"c"] intValue];
- if(code == 0){
- NSDictionary *data = (NSDictionary *)[dic objectForKey:@"d"];
-
- NSLog(@"Json解析结果 = %d %@", code, data);
- [self.view makeToast:@"发布成功" duration:2.0 position:CSToastPositionCenter];
- [self.navigationController popViewControllerAnimated:YES];
-
- [User sharedInfo].hasNewSocial = YES;
- }
- else{
- [self.view makeToast:@"发布失败" duration:2.0 position:CSToastPositionCenter];
- }
- } failure:^(NSURLSessionDataTask *task, NSError * error) {
- [SVProgressHUD dismiss];
- NSLog(@"请求失败,服务器返回的错误信息%@",error);
- [self.view makeToast:@"发布失败" duration:2.0 position:CSToastPositionCenter];
- }];
- }
-
- @end
|