9
9
#import " DBCameraSegueViewController.h"
10
10
#import " DBCameraBaseCropViewController+Private.h"
11
11
#import " DBCameraCropView.h"
12
+ #import " DBCameraFiltersView.h"
13
+ #import " DBCameraFilterCell.h"
14
+ #import " DBCameraLoadingView.h"
12
15
#import " UIImage+TintColor.h"
13
16
17
+ #import < GPUImage/GPUImage.h>
18
+
19
+ #define kFilterCellIdentifier @" filterCell"
20
+
14
21
#ifndef DBCameraLocalizedStrings
15
22
#define DBCameraLocalizedStrings (key ) \
16
23
NSLocalizedStringFromTable (key, @" DBCamera" , nil )
17
24
#endif
18
25
19
26
#define buttonMargin 20 .0f
20
27
21
- @interface DBCameraSegueViewController () <UIActionSheetDelegate> {
28
+ static const CGSize kFilterCellSize = { 75 , 90 };
29
+
30
+ @interface DBCameraSegueViewController () <UIActionSheetDelegate, UICollectionViewDelegate, UICollectionViewDataSource> {
22
31
DBCameraCropView *_cropView;
23
- NSArray *_cropArray;
32
+
33
+ NSArray *_cropArray, *_filtersList;
34
+ NSDictionary *_filterMapping;
24
35
CGRect _pFrame, _lFrame;
25
36
}
26
37
27
38
@property (nonatomic , strong ) UIView *navigationBar, *bottomBar;
28
39
@property (nonatomic , strong ) UIButton *useButton, *retakeButton, *cropButton;
40
+ @property (nonatomic , strong ) DBCameraLoadingView *loadingView;
29
41
@end
30
42
31
43
@implementation DBCameraSegueViewController
@@ -41,6 +53,13 @@ - (id) initWithImage:(UIImage *)image thumb:(UIImage *)thumb
41
53
if (self) {
42
54
// Custom initialization
43
55
_cropArray = @[ @320 , @213 , @240 , @192 , @180 ];
56
+ _filtersList = @[ @" normal" , @" 1977" , @" amaro" , @" grey" , @" hudson" , @" mayfair" , @" nashville" , @" valencia" ];
57
+ _filterMapping = @{ @0 :[[GPUImageFilter alloc ] init ], @1 :[[GPUImageToneCurveFilter alloc ] initWithACV: @" 1977" ],
58
+ @2 :[[GPUImageToneCurveFilter alloc ] initWithACV: @" amaro" ], @3 :[[GPUImageGrayscaleFilter alloc ] init ],
59
+ @4 :[[GPUImageToneCurveFilter alloc ] initWithACV: @" Hudson" ], @5 :[[GPUImageToneCurveFilter alloc ] initWithACV: @" mayfair" ],
60
+ @6 :[[GPUImageToneCurveFilter alloc ] initWithACV: @" Nashville" ], @7 :[[GPUImageToneCurveFilter alloc ] initWithACV: @" Valencia" ] };
61
+
62
+ _selectedFilterIndex = 0 ;
44
63
45
64
[self setSourceImage: image];
46
65
[self setPreviewImage: thumb];
@@ -59,16 +78,18 @@ - (void)viewDidLoad
59
78
60
79
[self .view setUserInteractionEnabled: YES ];
61
80
[self .view setBackgroundColor: [UIColor blackColor ]];
62
- [self .view addSubview: self .navigationBar];
63
- [self .view addSubview: self .bottomBar];
64
- [self .view setClipsToBounds: YES ];
65
81
66
82
CGFloat cropX = ( CGRectGetWidth ( self.frameView .frame ) - 320 ) * .5 ;
67
83
_pFrame = (CGRect ){ cropX, ( CGRectGetHeight ( self.frameView .frame ) - 360 ) * .5 , 320 , 320 };
68
84
_lFrame = (CGRect ){ cropX, ( CGRectGetHeight ( self.frameView .frame ) - 240 ) * .5 , 320 , 240 };
69
85
70
86
[self setCropRect: self .previewImage.size.width > self .previewImage.size.height ? _lFrame : _pFrame];
71
87
88
+ [self .view addSubview: self .filtersView];
89
+ [self .view addSubview: self .navigationBar];
90
+ [self .view addSubview: self .bottomBar];
91
+ [self .view setClipsToBounds: YES ];
92
+
72
93
if ( self.cameraSegueConfigureBlock )
73
94
self.cameraSegueConfigureBlock (self);
74
95
}
@@ -109,9 +130,10 @@ - (void) openActionsheet:(UIButton *)button
109
130
110
131
- (void ) createInterface
111
132
{
112
- CGFloat viewHeight = CGRectGetHeight ([[UIScreen mainScreen ] bounds ]) - 64 ;
133
+ CGFloat viewHeight = CGRectGetHeight ([[UIScreen mainScreen ] bounds ]) - 64 - 50 ;
113
134
_cropView = [[DBCameraCropView alloc ] initWithFrame: (CGRect ){ 0 , 64 , 320 , viewHeight }];
114
135
[_cropView setHidden: YES ];
136
+
115
137
[self setFrameView: _cropView];
116
138
}
117
139
@@ -127,8 +149,10 @@ - (void) saveImage
127
149
if ( [_delegate respondsToSelector: @selector (camera:didFinishWithImage:withMetadata: )] ) {
128
150
if ( _cropMode )
129
151
[self cropImage ];
130
- else
131
- [_delegate camera: self didFinishWithImage: self .sourceImage withMetadata: self .capturedImageMetadata];
152
+ else {
153
+ UIImage *transform = [_filterMapping[@(_selectedFilterIndex.row)] imageByFilteringImage: self .sourceImage];
154
+ [_delegate camera: self didFinishWithImage: transform withMetadata: self .capturedImageMetadata];
155
+ }
132
156
}
133
157
}
134
158
@@ -145,6 +169,7 @@ - (void) cropImage
145
169
dispatch_async (dispatch_get_main_queue (), ^{
146
170
UIImage *transform = [UIImage imageWithCGImage: resultRef scale: 1.0 orientation: UIImageOrientationUp];
147
171
CGImageRelease (resultRef);
172
+ transform = [_filterMapping[@(_selectedFilterIndex.row)] imageByFilteringImage: transform];
148
173
[_delegate camera: self didFinishWithImage: transform withMetadata: self .capturedImageMetadata];
149
174
});
150
175
});
@@ -155,6 +180,29 @@ - (void) setCropMode:(BOOL)cropMode
155
180
_cropMode = cropMode;
156
181
[self .frameView setHidden: !_cropMode];
157
182
[self .bottomBar setHidden: !_cropMode];
183
+ [self .filtersView setHidden: _cropMode];
184
+ }
185
+
186
+ - (DBCameraFiltersView *) filtersView
187
+ {
188
+ if ( !_filtersView ) {
189
+ _filtersView = [[DBCameraFiltersView alloc ] initWithFrame: (CGRect ){ 0 , CGRectGetHeight (self.view .frame )-kFilterCellSize .height , CGRectGetWidth (self.view .frame ), kFilterCellSize .height } collectionViewLayout: [DBCameraFiltersView filterLayout ]];
190
+ [_filtersView setDelegate: self ];
191
+ [_filtersView setDataSource: self ];
192
+ [_filtersView registerClass: [DBCameraFilterCell class ] forCellWithReuseIdentifier: kFilterCellIdentifier ];
193
+ }
194
+
195
+ return _filtersView;
196
+ }
197
+
198
+ - (DBCameraLoadingView *) loadingView
199
+ {
200
+ if ( !_loadingView ) {
201
+ _loadingView = [[DBCameraLoadingView alloc ] initWithFrame: (CGRect ){ 0 , 0 , 100 , 100 }];
202
+ [_loadingView setCenter: self .view.center];
203
+ }
204
+
205
+ return _loadingView;
158
206
}
159
207
160
208
- (UIView *) navigationBar
@@ -194,7 +242,7 @@ - (UIButton *) useButton
194
242
{
195
243
if ( !_useButton ) {
196
244
_useButton = [self baseButton ];
197
- [_useButton setTitle: DBCameraLocalizedStrings (@" button.use" ) forState: UIControlStateNormal];
245
+ [_useButton setTitle: [ DBCameraLocalizedStrings (@" button.use" ) uppercaseString ] forState: UIControlStateNormal];
198
246
[_useButton.titleLabel sizeToFit ];
199
247
[_useButton sizeToFit ];
200
248
[_useButton setFrame: (CGRect ){ CGRectGetWidth (self.view .frame ) - (CGRectGetWidth (_useButton.frame ) + buttonMargin), 0 , CGRectGetWidth (_useButton.frame ) + buttonMargin, 60 }];
@@ -208,7 +256,7 @@ - (UIButton *) retakeButton
208
256
{
209
257
if ( !_retakeButton ) {
210
258
_retakeButton = [self baseButton ];
211
- [_retakeButton setTitle: DBCameraLocalizedStrings (@" button.retake" ) forState: UIControlStateNormal];
259
+ [_retakeButton setTitle: [ DBCameraLocalizedStrings (@" button.retake" ) uppercaseString ] forState: UIControlStateNormal];
212
260
[_retakeButton.titleLabel sizeToFit ];
213
261
[_retakeButton sizeToFit ];
214
262
[_retakeButton setFrame: (CGRect ){ 0 , 0 , CGRectGetWidth (_retakeButton.frame ) + buttonMargin, 60 }];
@@ -237,6 +285,7 @@ - (UIButton *) baseButton
237
285
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
238
286
[button setBackgroundColor: [UIColor clearColor ]];
239
287
[button setTitleColor: self .tintColor forState: UIControlStateNormal];
288
+ button.titleLabel .font = [UIFont systemFontOfSize: 12 ];
240
289
241
290
return button;
242
291
}
@@ -246,6 +295,42 @@ - (BOOL) prefersStatusBarHidden
246
295
return YES ;
247
296
}
248
297
298
+ #pragma mark - UICollectionViewDataSource
299
+
300
+ - (NSInteger ) collectionView : (UICollectionView *)collectionView numberOfItemsInSection : (NSInteger )section
301
+ {
302
+ return _filtersList.count ;
303
+ }
304
+
305
+ - (UICollectionViewCell *) collectionView : (UICollectionView *)collectionView cellForItemAtIndexPath : (NSIndexPath *)indexPath
306
+ {
307
+ DBCameraFilterCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: kFilterCellIdentifier forIndexPath: indexPath];
308
+ [cell.imageView setImage: [UIImage imageNamed: [NSString stringWithFormat: @" %@ Filter" , _filtersList[indexPath.row]]]];
309
+ [cell.label setText: [_filtersList[indexPath.row] uppercaseString ]];
310
+ [cell.imageView.layer setBorderWidth: (self .selectedFilterIndex.row == indexPath.row) ? 1.0 : 0.0 ];
311
+
312
+ return cell;
313
+ }
314
+
315
+ - (CGSize ) collectionView : (UICollectionView *)collectionView layout : (UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath : (NSIndexPath *)indexPath
316
+ {
317
+ return kFilterCellSize ;
318
+ }
319
+
320
+ #pragma mark - UICollectionViewDelegate
321
+
322
+ - (void ) collectionView : (UICollectionView *)collectionView didSelectItemAtIndexPath : (NSIndexPath *)indexPath
323
+ {
324
+ [self .view addSubview: self .loadingView];
325
+
326
+ _selectedFilterIndex = indexPath;
327
+ [self .filtersView reloadData ];
328
+
329
+ UIImage *filteredImage = [_filterMapping[@(indexPath.row)] imageByFilteringImage: self .sourceImage];
330
+ [self .loadingView removeFromSuperview ];
331
+ [self .imageView setImage: filteredImage];
332
+ }
333
+
249
334
#pragma mark - UIActionSheetDelegate
250
335
251
336
- (void ) actionSheet : (UIActionSheet *)actionSheet clickedButtonAtIndex : (NSInteger )buttonIndex
0 commit comments