//
// ReleaseViewController.m
// RoadVehicles
//
// Created by ENERGY on 2018/3/8.
// Copyright © 2018年 appleLJ. All rights reserved.
//
#import "FeelingViewController.h"
#import "IWTextView.h"
#import "MJPhotoBrowser.h"
#import "MJPhoto.h"
#import "ZYQAssetPickerController.h"
#import "TestCellCollectionViewCell.h"
@interface FeelingViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate, MJPhotoBrowserDelegate,ZYQAssetPickerControllerDelegate>
@property (nonatomic, strong)IWTextView * textView;
@property (nonatomic, strong)UICollectionView * collectionView;
@property (nonatomic,strong) NSMutableArray *dataArray; //数据源
@property (nonatomic,strong) NSMutableArray *dataImageArray; // 存储选择的图片(二进制)
@property (nonatomic,strong) NSMutableArray *dataImageWithStrArray; // 上传图片返回的图片地址
@end
@implementation FeelingViewController(networking)
@end
@implementation FeelingViewController
- (void)viewDidLoad{
[super viewDidLoad];
[self.view addSubview:self.textView];
[self.view addSubview:self.collectionView];
}
- (IWTextView *)textView{
if (!_textView) {
_textView = [[IWTextView alloc] initWithFrame:CGRectMake(5/WIDTH_5S_SCALE, 5/WIDTH_5S_SCALE, SCREEN_WIDTH - 20/WIDTH_5S_SCALE, 100/WIDTH_5S_SCALE)];
_textView.placeholder = @"这一刻心情……";
_textView.font = DEF_FontSize_14;
}return _textView;
}
- (UICollectionView *)collectionView{
if (!_collectionView) {
UILabel * titleLab = [[UILabel alloc] initWithFrame:CGRectMake(self.textView.left, self.textView.bottom + 10/WIDTH_5S_SCALE, self.textView.width, 20/WIDTH_5S_SCALE)];
titleLab.text = @"上传图片(最多九张)";
titleLab.font = [UIFont systemFontOfSize:14.0];
titleLab.textColor = [UIColor blackColor];
[self.view addSubview:titleLab];
UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 10/WIDTH_5S_SCALE;
layout.minimumInteritemSpacing = 10/WIDTH_5S_SCALE;
layout.itemSize = CGSizeMake(80/WIDTH_5S_SCALE, 80/WIDTH_5S_SCALE);
layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, titleLab.bottom + 5/WIDTH_5S_SCALE, SCREEN_WIDTH, 100/WIDTH_5S_SCALE) collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor orangeColor];
[_collectionView registerClass:[TestCellCollectionViewCell class] forCellWithReuseIdentifier:NSStringFromClass([TestCellCollectionViewCell class])];
_collectionView.showsVerticalScrollIndicator = NO;
_collectionView.showsHorizontalScrollIndicator = NO;
}return _collectionView;
}
//打开相册选择照片
- (void)setupPhotoSelect{
ZYQAssetPickerController * picker = [[ZYQAssetPickerController alloc] init];
picker.maximumNumberOfSelection = 9-self.dataArray.count;;
picker.delegate = self;
picker.assetsFilter = [ALAssetsFilter allPhotos];
picker.showEmptyGroups = NO;
picker.selectionFilter = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings){
if ([[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyType] isEqual:ALAssetTypeVideo]){
NSTimeInterval duration = [[(ALAsset *)evaluatedObject valueForProperty:ALAssetPropertyDuration] doubleValue];
return duration >= 5;
} else {
return YES;
}
}];
[self presentViewController:picker animated:YES completion:nil];
}
//- (void)setUpPhotoBrower{
// MJPhotoBrowser * photoBrower = [[MJPhotoBrowser alloc] init];
//
//}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (self.dataArray.count >8) {
return 9;
}
return self.dataArray.count + 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
TestCellCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([TestCellCollectionViewCell class]) forIndexPath:indexPath];
if (indexPath.row != self.dataArray.count) {
cell.img = self.dataArray[indexPath.row];
}else{
cell.img = nil;
}
cell.deleteImgBlock = ^(){
[self.dataArray removeObjectAtIndex:indexPath.row];
[self.collectionView reloadData];
};
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (self.dataArray.count<9) {
if (self.dataArray.count != 0) {
if (indexPath.row == self.dataArray.count) {
// [self presentViewController:picker animated:YES completion:nil];
[self setupPhotoSelect];
}else{
//点击查看大图
//1.创建图片浏览器
MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init];
//2.告诉图片浏览器显示所有的图片
NSMutableArray *photos = [NSMutableArray array];
for (int i = 0 ; i < self.dataArray.count; i++) {
//传递数据给浏览器
MJPhoto *mjPhoto = [[MJPhoto alloc] init];
mjPhoto.image = self.dataArray[i];
TestCellCollectionViewCell *cell = (TestCellCollectionViewCell*)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
mjPhoto.srcImageView = cell.imageView; //设置来源哪一个UIImageView
[photos addObject:mjPhoto];
}
brower.photos = photos;
brower.isShowDeleteButton = YES;
brower.delegate = self;
//3.设置默认显示的图片索引
brower.currentPhotoIndex = indexPath.row;
//4.显示浏览器
[brower show];
}
}else{
[self setupPhotoSelect];
}
}
}
//ZYQ代理方法
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
for (int i=0; i< assets.count; i++) {
ALAsset * asset= assets[i];
UIImage * tempImg = [UIImage imageWithCGImage:asset.defaultRepresentation.fullScreenImage];
NSData * imageData;
imageData = UIImageJPEGRepresentation(tempImg, 0.3);
[self.dataImageWithStrArray addObject:imageData];
//添加未压缩的图片
[self.dataArray addObject:tempImg];
dispatch_async(dispatch_get_main_queue(), ^{
[self.collectionView reloadData];
NSLog(@"图片添加成功%@",tempImg);
});
asset = nil;
}
}
//brwoer代理方法
- (void)photoBrowser:(MJPhotoBrowser *)photoBrowser didDeletePhotoAtIndex:(NSInteger)index{
[self.dataArray removeObjectAtIndex:index];
[self.collectionView reloadData];
}
- (void)photoBrowser:(MJPhotoBrowser *)photoBrowser didChangedToPageAtIndex:(NSUInteger)index{
}
- (NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSMutableArray array].mutableCopy;
}return _dataArray;
}
- (NSMutableArray *)dataImageWithStrArray{
if (!_dataImageWithStrArray) {
_dataImageWithStrArray = [NSMutableArray array].mutableCopy;
}return _dataImageWithStrArray;
}
- (NSMutableArray *)dataImageArray{
if (!_dataImageArray) {
_dataImageArray = [NSMutableArray array].mutableCopy;
}return _dataImageArray;
}
@end