//
//? 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; //數(shù)據(jù)源
@property (nonatomic,strong) NSMutableArray *dataImageArray; // 存儲選擇的圖片(二進(jìn)制)
@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{
? ? ? ? ? ? ? ? //點(diǎn)擊查看大圖
? ? ? ? ? ? ? ? //1.創(chuàng)建圖片瀏覽器
? ? ? ? ? ? ? ? MJPhotoBrowser *brower = [[MJPhotoBrowser alloc] init];
? ? ? ? ? ? ? ? //2.告訴圖片瀏覽器顯示所有的圖片
? ? ? ? ? ? ? ? NSMutableArray *photos = [NSMutableArray array];
? ? ? ? ? ? ? ? for (int i = 0 ; i < self.dataArray.count; i++) {
? ? ? ? ? ? ? ? ? ? //傳遞數(shù)據(jù)給瀏覽器
? ? ? ? ? ? ? ? ? ? MJPhoto *mjPhoto = [[MJPhoto alloc] init];
? ? ? ? ? ? ? ? ? ? mjPhoto.image = self.dataArray[i];
? ? ? ? ? ? ? ? ? ? TestCellCollectionViewCell *cell = (TestCellCollectionViewCell*)[collectionView cellForItemAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]];
? ? ? ? ? ? ? ? ? ? mjPhoto.srcImageView = cell.imageView; //設(shè)置來源哪一個UIImageView
? ? ? ? ? ? ? ? ? ? [photos addObject:mjPhoto];
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? brower.photos = photos;
? ? ? ? ? ? ? ? brower.isShowDeleteButton = YES;
? ? ? ? ? ? ? ? brower.delegate = self;
? ? ? ? ? ? ? ? //3.設(shè)置默認(rèn)顯示的圖片索引
? ? ? ? ? ? ? ? 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