| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- //
- // PublishViewController.m
- // model
- //
- // Created by liufei on 2018/7/25.
- // Copyright © 2018年 Mine. All rights reserved.
- //
- #import "PublishViewController.h"
- #import "UITextView+WZB.h"
- #import "PhotoCollectionViewCell.h"
- @interface PublishViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- {
- UIImagePickerController *pickerVc;
- }
- @property (nonatomic, strong) UITextView *pubTextView;
- @property (nonatomic, strong) NSMutableArray *dataSource;
- @property (nonatomic, strong) UICollectionView *photoListView;
- @property (nonatomic, strong) NSMutableArray *imageSource;
- @end
- @implementation PublishViewController
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- // [self.navigationController.navigationBar setShadowImage:[UIImage new]];
- self.navigationController.navigationBar.translucent = NO;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- self.imageSource = [NSMutableArray new];
- self.dataSource = [NSMutableArray new];
- [self configUI];
- // Do any additional setup after loading the view.
- }
- - (void)configUI {
- self.view.backgroundColor = [UIColor whiteColor];
- self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
- self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(handleCancel)];
- [self.navigationItem.leftBarButtonItem setTintColor:[UIColor blackColor]];
-
- self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"发表" style:UIBarButtonItemStylePlain target:self action:@selector(handlePublish)];
- [self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithHexString:@"#fe4086"]];
- self.pubTextView = [[UITextView alloc] initWithFrame:CGRectMake(40, 40, ScreenWidth - 80, 76)];
- self.pubTextView.wzb_placeholder = @"这一刻的想法...";
- self.pubTextView.font = [UIFont systemFontOfSize:16];
- [self.view addSubview:self.pubTextView];
-
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
- layout.sectionInset = UIEdgeInsetsMake(0, 40, 10, 40);
- layout.itemSize = CGSizeMake(ScreenWidth/3 - 30, ScreenWidth/3 - 30);
- layout.minimumLineSpacing = 5;
- layout.minimumInteritemSpacing = 5;
- self.photoListView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.pubTextView.frame), ScreenWidth, ScreenWidth *2/3 - 50) collectionViewLayout:layout];
- self.photoListView.delegate = self;
- self.photoListView.dataSource = self;
- self.photoListView.backgroundColor = [UIColor whiteColor];
- [self.photoListView registerNib:[UINib nibWithNibName:@"PhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class])];
- [self.view addSubview:self.photoListView];
- }
- #pragma mark -
- - (void)handleSave {
-
- }
- - (void)handleCancel{
- [self.navigationController popViewControllerAnimated:YES];
- }
- - (void)handlePublish {
- if (self.pubTextView.text.length==0&&self.imageSource.count==0) {
- [MBProgressHUD showTextHUD:@"请输入动态内容" inView:self.view hideAfterDelay:1];
- }else{
- [self firstPublish];
- }
- }
- - (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
- if (![self.pubTextView isExclusiveTouch]) {
- [self.pubTextView resignFirstResponder];
- }
- }
- - (void)firstPublish{
- NSString *str = [NSString stringWithFormat:@"%@modelInfo?action=doAddPrivate&ModelPK=%@&Intro=%@",imageURl,[ModelUser user].modelpk,self.pubTextView.text];
- [[AHHttpManager sharedManager]POST:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(id responseObject) {
- NSLog(@"%@",responseObject);
- if ([[responseObject objectForKey:@"msg"]isEqualToString:@"success"]) {
- NSString *privatePk = [responseObject objectForKey:@"privatepk"];
- if(self.imageSource.count==0){
- [self.navigationController popViewControllerAnimated:YES];
- }else{
- [self haveImagePublish:privatePk];
- }
- }else{
- [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
- }
- } failure:^(NSError *error) {
- NSLog(@"error = %@",error);
- [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
- }];
- }
- - (void)haveImagePublish:(NSString*)privatePk{
- NSString * urlStr;
- for (int i=0; i<self.imageSource.count; i++) {
- if (i==0) {
- urlStr = self.imageSource[0];
- }else{
- urlStr = [NSString stringWithFormat:@"%@,%@",urlStr,self.imageSource[i]];
- }
- }
-
- NSString *str = [NSString stringWithFormat:@"%@modelInfo?action=doAddPrivateItem&ModelPK=%@&PrivatePK=%@&URL=%@&FType=图片",imageURl,[ModelUser user].modelpk,privatePk,urlStr];
- [[AHHttpManager sharedManager]POST:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil success:^(id responseObject) {
- NSLog(@"%@",responseObject);
- if ([[responseObject objectForKey:@"msg"]isEqualToString:@"success"]) {
- [self.navigationController popViewControllerAnimated:YES];
- }else{
- [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
- }
- } failure:^(NSError *error) {
- NSLog(@"error = %@",error);
- [MBProgressHUD showTextHUD:@"超时" inView:self.view hideAfterDelay:1];
- }];
- }
- #pragma mark -
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
- return self.dataSource.count < 4 ? self.dataSource.count + 1:4;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
- PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class]) forIndexPath:indexPath];
- if (indexPath.item < self.dataSource.count) {
- cell.userImageView.image = self.dataSource[indexPath.item];
- }
-
- return cell;
-
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- if (self.dataSource.count == indexPath.item) {
- UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
- pickerVc = [[UIImagePickerController alloc] init];
- pickerVc.allowsEditing = YES;
- pickerVc.delegate = self;
- UIAlertAction *library = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- pickerVc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- [self presentViewController:pickerVc animated:YES completion:nil];
-
- }];
- UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
- pickerVc.sourceType = UIImagePickerControllerSourceTypeCamera;
- [self presentViewController:pickerVc animated:YES completion:nil];
- }];
- UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
- [alertVc addAction:library];
- [alertVc addAction:camera];
- [alertVc addAction:cancel];
- [self presentViewController:alertVc animated:YES completion:nil];
- }
- }
- #pragma mark -
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
- UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
- [picker dismissViewControllerAnimated:NO completion:nil];
- [self postPicDataWithNetworkingWith:image];
- }
- #pragma mark -- 上传图片
- - (void)postPicDataWithNetworkingWith:(UIImage *)image {
- [MBProgressHUD showHUDAddedTo:self.view animated:YES];
-
- [YanCNetWorkManager requestPostWithURLStr:Url_skillphoto(PublicUrl) fileData:image name:@"img" fileName:@"111.png" mimeType:@"image/jpg/png" parameters:nil finish:^(id dataDic) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
-
- NSString *issuccess = dataDic[@"msg"];
- if ([issuccess isEqualToString:@"success"]) {
- NSString *pubImageUrl = dataDic[@"img"];
- [self.imageSource addObject:pubImageUrl];
- [self.dataSource addObject:image];
- [self.photoListView reloadData];
- } else {
- [MBProgressHUD showInfo:@"请求失败!"];
- }
- } enError:^(NSError *error) {
- [MBProgressHUD hideHUDForView:self.view animated:YES];
- }];
- }
- - (NSMutableArray *)dataSource {
- if (!_dataSource) {
- _dataSource = [NSMutableArray array];
- }
- return _dataSource;
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|