在collectionView中點(diǎn)擊上傳圖片,點(diǎn)擊item變大圖或刪除

//

//? 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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末空繁,一起剝皮案震驚了整個濱河市把敞,隨后出現(xiàn)的幾起案子落塑,更是在濱河造成了極大的恐慌纽疟,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件憾赁,死亡現(xiàn)場離奇詭異污朽,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)龙考,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進(jìn)店門蟆肆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人洲愤,你說我怎么就攤上這事颓芭。” “怎么了柬赐?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長官紫。 經(jīng)常有香客問我肛宋,道長,這世上最難降的妖魔是什么束世? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任酝陈,我火速辦了婚禮,結(jié)果婚禮上毁涉,老公的妹妹穿的比我還像新娘沉帮。我一直安慰自己,他們只是感情好贫堰,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布穆壕。 她就那樣靜靜地躺著,像睡著了一般其屏。 火紅的嫁衣襯著肌膚如雪喇勋。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天偎行,我揣著相機(jī)與錄音川背,去河邊找鬼贰拿。 笑死,一個胖子當(dāng)著我的面吹牛熄云,可吹牛的內(nèi)容都是我干的膨更。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼缴允,長吁一口氣:“原來是場噩夢啊……” “哼荚守!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起癌椿,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤健蕊,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后踢俄,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體缩功,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年都办,在試婚紗的時候發(fā)現(xiàn)自己被綠了嫡锌。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡琳钉,死狀恐怖势木,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情歌懒,我是刑警寧澤啦桌,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站及皂,受9級特大地震影響甫男,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜验烧,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一板驳、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧碍拆,春花似錦若治、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至浩习,卻和暖如春静暂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背谱秽。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工洽蛀, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留摹迷,地道東北人。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓郊供,卻偏偏與公主長得像峡碉,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子驮审,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容