1 //
2 // MainViewController.m
3 // AddPhoto
4 //
5 // Created by ChenJungang on 14/11/20.
6 // Copyright (c) 2014年 ChenJungang. All rights reserved.
7 //
8
9 #import "MainViewController.h"
10 #import "CustomFlowLayout.h"
11 #import "PhotoCell.h"
12 #import "ResuableView.h"
13 #import <MobileCoreServices/MobileCoreServices.h>
14 #import "DetailViewController.h"
15
16
17 #define kImage @"kImage"
18 #define kImageData @"kImageData"
19 #define kImageReferenceUrl @"kImageReferenceUrl"
20
21
22 static NSString *itemIdentifier = @"ItemIdentifier";
23 static NSString *footerReuseId = @"footerReuseId";
24
25 @interface MainViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,ResuableViewDelegate,UIActionSheetDelegate>
26
27 @property (strong, nonatomic)UICollectionView *collectionView;
28 @property (strong, nonatomic)CustomFlowLayout *customFlowLayout;
29 @property (strong, nonatomic)NSMutableArray *imageArray;
30 @property (strong, nonatomic)NSIndexPath *indexPath;
31
32 @end
33
34 @implementation MainViewController
35
36 - (instancetype)init{
37 self = [super init];
38 if (self) {
39 _imageArray = [NSMutableArray array];
40 }
41 return self;
42 }
43
44 - (void)viewDidLoad {
45 [super viewDidLoad];
46 // Do any additional setup after loading the view from its nib.
47 self.title = @"add photo";
48
49 _customFlowLayout = [CustomFlowLayout new];
50 _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 64, [[UIScreen mainScreen] bounds].size.width - 20, 60)
51 collectionViewLayout:_customFlowLayout];
52 _collectionView.backgroundColor = [UIColor whiteColor];
53
54 if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
55 self.edgesForExtendedLayout = UIRectEdgeNone;
56 }
57
58 [self.collectionView registerClass:[PhotoCell class]
59 forCellWithReuseIdentifier:itemIdentifier];
60
61 [self.collectionView registerClass:[ResuableView class]
62 forSupplementaryViewOfKind:UICollectionElementKindSectionFooter
63 withReuseIdentifier:footerReuseId];
64
65 _collectionView.dataSource = self;
66 _collectionView.delegate = self;
67
68 [self.view addSubview:_collectionView];
69 }
70
71 #pragma mark - UICollectionViewDataSource
72 -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
73 {
74 return [_imageArray count];
75 }
76
77 -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
78 {
79 PhotoCell *cell =
80 [collectionView dequeueReusableCellWithReuseIdentifier:itemIdentifier
81 forIndexPath:indexPath];
82
83 if ([_imageArray count] != 0) {
84 cell.showImageView.image = _imageArray[indexPath.row][kImage];
85 }
86
87 return cell;
88 }
89
90 #pragma mark - UICollectionViewDelegate
91
92 - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
93 NSLog(@"row : %d",indexPath.row);
94
95
96 _indexPath = indexPath;
97 UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"操作" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"删除" otherButtonTitles:@"查看", nil];
98 [actionSheet showInView:self.view];
99
100 }
101
102 - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
103
104 ResuableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
105 withReuseIdentifier:footerReuseId
106 forIndexPath:indexPath];
107 _indexPath = indexPath;
108 footerView.delegate = self;
109
110 return footerView;
111 }
112
113 #pragma mark - UIActionSheetDelegate
114
115 - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
116
117 switch (buttonIndex) {
118 case 0:{
119 if ([_imageArray count] != 0) {
120 [_imageArray removeObjectAtIndex:_indexPath.row];
121 [self.collectionView reloadData];
122 }
123 break;
124 }
125 case 1:{
126 DetailViewController *detailVC = [[DetailViewController alloc] init];
127 detailVC.image = _imageArray[_indexPath.row][kImage];
128 [self presentViewController:detailVC animated:YES completion:nil];
129 break;
130 }
131 default:
132 break;
133 }
134 }
135
136
137 #pragma mark - UIImagePickerControllerDelegate
138
139 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
140 if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage]) {
141
142 if ([_imageArray count] != 0) {
143 for (NSDictionary *dic in _imageArray) {
144 if ([[info objectForKey:UIImagePickerControllerReferenceURL] isEqual:[dic objectForKey:kImageReferenceUrl]]) {
145 return [self dismissViewControllerAnimated:YES completion:^{
146 [MBHUDHelper showWarningWithText:@"该图片已经选择"];
147 }];
148 }
149 }
150 }
151 //取出选中图片的信息
152 UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
153 NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
154 NSString *referenceUrl = [info objectForKey:UIImagePickerControllerReferenceURL];
155
156 NSDictionary *imageInfoDic = @{kImage:image, kImageData:imageData, kImageReferenceUrl:referenceUrl};
157
158 [_imageArray addObject:imageInfoDic];
159 [self.collectionView reloadData];
160
161 //滚动到最右边
162 if ([_imageArray count] != 0) {
163 [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:[_imageArray indexOfObject:[_imageArray lastObject]] inSection:0] atScrollPosition:UICollectionViewScrollPositionRight animated:YES];
164 }
165 }
166 [picker dismissViewControllerAnimated:YES completion:nil];
167 }
168
169
170 #pragma mark - ResuableViewDelegate
171
172 - (void)clickAddPhotoBtnAction{
173 [self getPhotoFromPhotoLibrary:UIImagePickerControllerSourceTypePhotoLibrary];
174 }
175
176 -(void)getPhotoFromPhotoLibrary:(UIImagePickerControllerSourceType)sourceType
177 {
178 NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:sourceType];
179 if([UIImagePickerController isSourceTypeAvailable:sourceType] && mediaTypes.count > 0)
180 {
181 NSArray *mediaTypes=[UIImagePickerController availableMediaTypesForSourceType:sourceType];
182 UIImagePickerController *picker = [[UIImagePickerController alloc] init];
183 picker.mediaTypes = mediaTypes;
184 picker.delegate = self;
185 picker.allowsEditing = YES;
186 picker.sourceType = sourceType;
187 NSString *requiredMediaType = (NSString *)kUTTypeImage;
188 NSArray *arrMediaTypes = [NSArray arrayWithObject:requiredMediaType];
189 [picker setMediaTypes:arrMediaTypes];
190 [self presentViewController:picker animated:YES completion:nil];
191 }else{
192 UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"错误信息!" message:@"当前设备不支持拍摄功能" delegate:nil cancelButtonTitle:@"确认" otherButtonTitles: nil];
193 [alert show];
194 }
195 }
196
197 - (void)didReceiveMemoryWarning {
198 [super didReceiveMemoryWarning];
199 // Dispose of any resources that can be recreated.
200 }
201
202
203 @end