|
|
@@ -9,23 +9,17 @@
|
|
|
#import "ModelPhotoViewController.h"
|
|
|
#import "PhotoCollectionViewCell.h"
|
|
|
#import "PhotoModel.h"
|
|
|
-
|
|
|
-@interface ModelPhotoViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
|
|
|
-{
|
|
|
- UIImagePickerController *pickerVc ;
|
|
|
+#import "Masonry.h"
|
|
|
+#import "UICollectionViewLeftAlignedLayout.h"
|
|
|
+@interface ModelPhotoViewController () <UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate> {
|
|
|
+ UIImagePickerController *pickerVc;
|
|
|
}
|
|
|
|
|
|
-@property (nonatomic, strong) UICollectionView *photoListView;
|
|
|
-
|
|
|
-@property (nonatomic, strong) NSMutableArray *dataSource;
|
|
|
-
|
|
|
-@property (nonatomic, strong) UIButton *saveBtn;
|
|
|
-
|
|
|
-@property (nonatomic, strong) NSMutableArray *tempUrl;
|
|
|
-
|
|
|
-@property (nonatomic, strong) NSMutableArray *updateData;
|
|
|
-
|
|
|
-@property (nonatomic, strong) NSMutableArray *deleteData;
|
|
|
+@property(nonatomic, strong) UICollectionView *photoListView;
|
|
|
+@property(nonatomic, strong) NSMutableArray *dataSource;
|
|
|
+@property(nonatomic, strong) NSMutableArray *tempUrl;
|
|
|
+@property(nonatomic, strong) NSMutableArray *updateData;
|
|
|
+@property(nonatomic, strong) NSMutableArray *deleteData;
|
|
|
|
|
|
@end
|
|
|
|
|
|
@@ -66,103 +60,145 @@
|
|
|
}
|
|
|
|
|
|
- (void)getModelPhoto {
|
|
|
-
|
|
|
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
|
|
|
- [YanCNetWorkManager requestGETWithURLStr:Url_getModelPhoto(PublicUrl) parameters:@{@"modelpk":[ModelUser user].modelpk} finish:^(id dataDic) {
|
|
|
- [MBProgressHUD hideHUDForView:self.view animated:YES];
|
|
|
-
|
|
|
- if ([dataDic[@"msg"] isEqualToString:@"success"]) {
|
|
|
- for (NSDictionary *dic in dataDic[@"data"]) {
|
|
|
- PhotoModel *model = [PhotoModel mj_objectWithKeyValues:dic];
|
|
|
- [self.dataSource addObject:model];
|
|
|
- }
|
|
|
- [self.photoListView reloadData];
|
|
|
- }
|
|
|
- } enError:^(NSError *error) {
|
|
|
-
|
|
|
- }];
|
|
|
+ [YanCNetWorkManager requestGETWithURLStr:Url_getModelPhoto(PublicUrl)
|
|
|
+ parameters:@{@"modelpk": [ModelUser user].modelpk}
|
|
|
+ finish:^(id dataDic) {
|
|
|
+ [MBProgressHUD hideHUDForView:self.view animated:YES];
|
|
|
+ if ([dataDic[@"msg"] isEqualToString:@"success"]) {
|
|
|
+ for (NSDictionary *dic in dataDic[@"data"]) {
|
|
|
+ PhotoModel *model = [PhotoModel mj_objectWithKeyValues:dic];
|
|
|
+ [self.dataSource addObject:model];
|
|
|
+ }
|
|
|
+ [self.photoListView reloadData];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ enError:^(NSError *error) {
|
|
|
+
|
|
|
+ }];
|
|
|
}
|
|
|
|
|
|
- (void)configUI {
|
|
|
- self.view.backgroundColor = RGBValueColor(0xf7f7f7, 1.0);
|
|
|
+ self.view.backgroundColor = [UIColor colorWithHexString:@"#F2F4F5"];
|
|
|
self.title = @"照片";
|
|
|
self.navigationController.navigationBar.tintColor = RGBValueColor(0x333333, 1);
|
|
|
- //self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(handleSave)];
|
|
|
-
|
|
|
- UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
|
|
|
- layout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
|
|
|
- layout.itemSize = CGSizeMake(ScreenWidth/2 - 25, ScreenWidth/2 - 25);
|
|
|
- layout.minimumLineSpacing = 10;
|
|
|
- layout.minimumInteritemSpacing = 10;
|
|
|
-
|
|
|
- self.photoListView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenWidth) collectionViewLayout:layout];
|
|
|
+ UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(handleSave)];
|
|
|
+ rightItem.tintColor = [UIColor blackColor];
|
|
|
+ self.navigationItem.rightBarButtonItem = rightItem;
|
|
|
+
|
|
|
+ UICollectionViewFlowLayout *layout = [[UICollectionViewLeftAlignedLayout alloc] init];
|
|
|
+
|
|
|
+ self.photoListView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
|
|
|
self.photoListView.delegate = self;
|
|
|
self.photoListView.dataSource = self;
|
|
|
- self.photoListView.backgroundColor = [UIColor whiteColor];
|
|
|
+ self.photoListView.backgroundColor = [UIColor clearColor];
|
|
|
[self.photoListView registerNib:[UINib nibWithNibName:@"PhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class])];
|
|
|
+ [self.photoListView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView"];
|
|
|
[self.view addSubview:self.photoListView];
|
|
|
-
|
|
|
- UILabel *deleteLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.photoListView.frame), 200, 20)];
|
|
|
- deleteLabel.text = @"点击可删除照片";
|
|
|
- deleteLabel.textColor = RGB(102, 102, 102);
|
|
|
- deleteLabel.font = [UIFont systemFontOfSize:14];
|
|
|
- [self.view addSubview:deleteLabel];
|
|
|
-
|
|
|
- self.saveBtn = [[UIButton alloc] initWithFrame:CGRectMake(20,CGRectGetMaxY(self.photoListView.frame) + 40, ScreenWidth - 40, 48)];
|
|
|
- self.saveBtn.layer.cornerRadius = 24.f;
|
|
|
- self.saveBtn.layer.masksToBounds = YES;
|
|
|
- [self.saveBtn setTitle:@"保存" forState:UIControlStateNormal];
|
|
|
- [self.saveBtn addTarget:self action:@selector(handleSave) forControlEvents:UIControlEventTouchUpInside];
|
|
|
- [self.saveBtn setBackgroundImage:[UIImage imageNamed:@"tixian-1"] forState:UIControlStateNormal];
|
|
|
- [self.view addSubview:self.saveBtn];
|
|
|
+ [self.photoListView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
+ make.top.equalTo(self.view);
|
|
|
+ make.bottom.equalTo(self.view);
|
|
|
+ make.left.right.equalTo(self.view);
|
|
|
+ }];
|
|
|
}
|
|
|
|
|
|
|
|
|
-
|
|
|
- (void)backClick {
|
|
|
[self.navigationController popViewControllerAnimated:YES];
|
|
|
}
|
|
|
|
|
|
#pragma mark -collectionView
|
|
|
+
|
|
|
+- (CGSize)collectionView:(UICollectionView *)collectionView
|
|
|
+ layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
+ sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ if (indexPath.row == 0) {
|
|
|
+ return CGSizeMake(ScreenWidth - 40, ScreenWidth - 40);
|
|
|
+ } else {
|
|
|
+ return CGSizeMake((ScreenWidth - 50) / 2, (ScreenWidth - 50) / 2);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView
|
|
|
+ layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
+ insetForSectionAtIndex:(NSInteger)section {
|
|
|
+ return UIEdgeInsetsMake(10, 20, 20, 20);
|
|
|
+}
|
|
|
+
|
|
|
+- (CGFloat) collectionView:(UICollectionView *)collectionView
|
|
|
+ layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
+minimumLineSpacingForSectionAtIndex:(NSInteger)section {
|
|
|
+ return 10;
|
|
|
+}
|
|
|
+
|
|
|
+- (CGFloat) collectionView:(UICollectionView *)collectionView
|
|
|
+ layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
+minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
|
|
|
+ return 10;
|
|
|
+}
|
|
|
+
|
|
|
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
|
|
|
-
|
|
|
- return self.dataSource.count+1;
|
|
|
+ return self.dataSource.count + 1;
|
|
|
+}
|
|
|
+
|
|
|
+- (CGSize) collectionView:(UICollectionView *)collectionView
|
|
|
+ layout:(UICollectionViewLayout *)collectionViewLayout
|
|
|
+referenceSizeForHeaderInSection:(NSInteger)section {
|
|
|
+ return CGSizeMake(ScreenWidth, 35);
|
|
|
+}
|
|
|
+
|
|
|
+- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
|
|
|
+ viewForSupplementaryElementOfKind:(NSString *)kind
|
|
|
+ atIndexPath:(NSIndexPath *)indexPath {
|
|
|
+ if (kind == UICollectionElementKindSectionHeader) {
|
|
|
+ UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView" forIndexPath:indexPath];
|
|
|
+ for (UIView *view in [headerView subviews]) {
|
|
|
+ [view removeFromSuperview];
|
|
|
+ }
|
|
|
+ UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, ScreenWidth - 40, 20)];
|
|
|
+ label.text = @"点击可删除照片(最少上传5张)";
|
|
|
+ label.font = [UIFont systemFontOfSize:14];
|
|
|
+ label.textColor = [UIColor colorWithHexString:@"#666666"];
|
|
|
+ [headerView addSubview:label];
|
|
|
+ return headerView;
|
|
|
+ }
|
|
|
+ return nil;
|
|
|
}
|
|
|
|
|
|
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
- PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class]) forIndexPath:indexPath];
|
|
|
-
|
|
|
+ PhotoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([PhotoCollectionViewCell class])
|
|
|
+ forIndexPath:indexPath];
|
|
|
+ cell.layer.cornerRadius = 8;
|
|
|
+ cell.clipsToBounds = YES;
|
|
|
if (indexPath.item < self.dataSource.count) {
|
|
|
PhotoModel *model = self.dataSource[indexPath.item];
|
|
|
-
|
|
|
if (model.localImage) {
|
|
|
cell.userImageView.image = model.localImage;
|
|
|
} else {
|
|
|
- [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", PublicUrl,model.photo]]];
|
|
|
+ [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", PublicUrl, model.photo]]];
|
|
|
}
|
|
|
- }else{
|
|
|
- cell.userImageView.image = [UIImage imageNamed:@"photo_add"];
|
|
|
+ } else {
|
|
|
+ cell.userImageView.image = nil;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return cell;
|
|
|
}
|
|
|
|
|
|
-- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
|
|
|
-{
|
|
|
+- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
|
|
|
if (self.dataSource.count == indexPath.item) {
|
|
|
if (self.dataSource.count >= 9) {
|
|
|
[MBProgressHUD showSuccess:@"最多不能超过9张"];
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
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) {
|
|
|
+ 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) {
|
|
|
+ UIAlertAction *camera = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
|
|
|
pickerVc.sourceType = UIImagePickerControllerSourceTypeCamera;
|
|
|
[self presentViewController:pickerVc animated:YES completion:nil];
|
|
|
}];
|
|
|
@@ -172,10 +208,10 @@
|
|
|
[alertVc addAction:cancel];
|
|
|
[self presentViewController:alertVc animated:YES completion:nil];
|
|
|
}
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:nil message:@"是否确定删除" preferredStyle:UIAlertControllerStyleAlert];
|
|
|
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
|
|
|
- UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
|
|
|
+ UIAlertAction *sureAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *_Nonnull action) {
|
|
|
[self deletePhoto:indexPath.item];
|
|
|
}];
|
|
|
[alertVc addAction:cancelAction];
|
|
|
@@ -185,45 +221,53 @@
|
|
|
}
|
|
|
|
|
|
#pragma mark -imagePickerController
|
|
|
-- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
|
|
|
- UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
|
|
|
+
|
|
|
+- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *, id> *)info {
|
|
|
+ UIImage *image = info[@"UIImagePickerControllerEditedImage"];
|
|
|
[picker dismissViewControllerAnimated:NO completion:nil];
|
|
|
PhotoModel *model = [[PhotoModel alloc] init];
|
|
|
model.localImage = image;
|
|
|
-
|
|
|
+
|
|
|
NSData *photoData = UIImageJPEGRepresentation(image, 0.5);
|
|
|
NSInteger length = [photoData length] / 1000;
|
|
|
-
|
|
|
- NSLog(@"图片大小:%ld Kb",(long)length);
|
|
|
-
|
|
|
+
|
|
|
+ NSLog(@"图片大小:%ld Kb", (long) length);
|
|
|
+
|
|
|
[self.dataSource addObject:model];
|
|
|
[self.updateData addObject:image];
|
|
|
[self.photoListView reloadData];
|
|
|
-
|
|
|
+
|
|
|
[self uploadImage:image];
|
|
|
}
|
|
|
|
|
|
|
|
|
--(void)uploadImage:(UIImage *)image{
|
|
|
-
|
|
|
+- (void)uploadImage:(UIImage *)image {
|
|
|
+
|
|
|
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
|
|
|
-
|
|
|
- [YanCNetWorkManager requestPostWithURLStr:Url_uploadImage(PublicUrl) fileData:image name:@"img" fileName:@"file.jpg" mimeType:@"image/jpeg" parameters:nil finish:^(id dataDic) {
|
|
|
- [MBProgressHUD hideHUDForView:self.view animated:YES];
|
|
|
- [self.tempUrl addObject:dataDic[@"img"]];
|
|
|
-
|
|
|
- } enError:^(NSError *error) {
|
|
|
- [MBProgressHUD hideHUDForView:self.view animated:YES];
|
|
|
- }];
|
|
|
-
|
|
|
+ [YanCNetWorkManager requestPostWithURLStr:Url_uploadImage(PublicUrl)
|
|
|
+ fileData:image
|
|
|
+ name:@"img"
|
|
|
+ fileName:@"file.jpg"
|
|
|
+ mimeType:@"image/jpeg"
|
|
|
+ parameters:nil
|
|
|
+ finish:^(id dataDic) {
|
|
|
+ [MBProgressHUD hideHUDForView:self.view animated:YES];
|
|
|
+ [self.tempUrl addObject:dataDic[@"img"]];
|
|
|
+
|
|
|
+ }
|
|
|
+ enError:^(NSError *error) {
|
|
|
+ [MBProgressHUD hideHUDForView:self.view animated:YES];
|
|
|
+ }];
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
|
#pragma mark -
|
|
|
+
|
|
|
- (void)handleSave {
|
|
|
-
|
|
|
+
|
|
|
[self uploadImageThroughURLs:self.tempUrl];
|
|
|
-
|
|
|
+
|
|
|
// [MBProgressHUD showHUDAddedTo:self.view animated:YES];
|
|
|
// dispatch_group_t group = dispatch_group_create();
|
|
|
//
|
|
|
@@ -250,54 +294,54 @@
|
|
|
// });
|
|
|
}
|
|
|
|
|
|
--(void)uploadImageThroughURLs:(NSMutableArray *)urls{
|
|
|
-
|
|
|
+- (void)uploadImageThroughURLs:(NSMutableArray *)urls {
|
|
|
+
|
|
|
NSString *urlStr = @"";
|
|
|
-
|
|
|
+
|
|
|
if (urls.count > 0) {
|
|
|
urlStr = [urls firstObject];
|
|
|
-
|
|
|
+
|
|
|
if (urls.count > 1) {
|
|
|
- for (int i=1; i<urls.count; i++) {
|
|
|
- urlStr = [urlStr stringByAppendingFormat:@",%@",urls[i]];
|
|
|
+ for (int i = 1; i < urls.count; i++) {
|
|
|
+ urlStr = [urlStr stringByAppendingFormat:@",%@", urls[i]];
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- NSDictionary *dic = @{@"modelpk":[ModelUser user].modelpk,@"photo":urlStr};
|
|
|
-
|
|
|
+
|
|
|
+ NSDictionary *dic = @{@"modelpk": [ModelUser user].modelpk, @"photo": urlStr};
|
|
|
+
|
|
|
[YanCNetWorkManager requestPostWithURLStr:Url_addModelPhoto(PublicUrl) parameters:dic finish:^(id dataDic) {
|
|
|
if ([dataDic[@"msg"] isEqualToString:@"success"]) {
|
|
|
[MBProgressHUD showSuccess:@"保存成功"];
|
|
|
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
|
|
|
}
|
|
|
- } enError:^(NSError *error) {
|
|
|
-
|
|
|
+ } enError:^(NSError *error) {
|
|
|
+
|
|
|
}];
|
|
|
- }else{
|
|
|
+ } else {
|
|
|
[MBProgressHUD showSuccess:@"保存成功"];
|
|
|
[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(backClick) userInfo:nil repeats:NO];
|
|
|
}
|
|
|
}
|
|
|
|
|
|
--(void)deletePhoto:(NSInteger)index{
|
|
|
-
|
|
|
- PhotoModel *model = [self.dataSource objectAtIndex:index];
|
|
|
-
|
|
|
+- (void)deletePhoto:(NSInteger)index {
|
|
|
+
|
|
|
+ PhotoModel *model = self.dataSource[index];
|
|
|
+
|
|
|
if (model.localImage) {
|
|
|
- for (UIImage *img in self.updateData ) {
|
|
|
+ for (UIImage *img in self.updateData) {
|
|
|
if (img == model.localImage) {
|
|
|
[self.updateData removeObject:img];
|
|
|
}
|
|
|
}
|
|
|
- }else{
|
|
|
- NSDictionary *dic = @{@"modelpk":[ModelUser user].modelpk,@"modelphotopk":model.modelphotopk};
|
|
|
-
|
|
|
+ } else {
|
|
|
+ NSDictionary *dic = @{@"modelpk": [ModelUser user].modelpk, @"modelphotopk": model.modelphotopk};
|
|
|
+
|
|
|
[YanCNetWorkManager requestPostWithURLStr:Url_deleteModelPhoto(PublicUrl) parameters:dic finish:^(id dataDic) {
|
|
|
if ([dataDic[@"msg"] isEqualToString:@"success"]) {
|
|
|
-
|
|
|
+
|
|
|
}
|
|
|
- } enError:^(NSError *error) {
|
|
|
-
|
|
|
+ } enError:^(NSError *error) {
|
|
|
+
|
|
|
}];
|
|
|
}
|
|
|
[self.dataSource removeObjectAtIndex:index];
|