-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBounceGalleryViewController.m
351 lines (274 loc) · 9.77 KB
/
BounceGalleryViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
//
// BounceGalleryViewController.m
//
// Created by Eli Gregory on 4/13/15.
// Copyright (c) 2015 Stublisher Inc. All rights reserved.
//
#import "BounceGalleryViewController.h"
#import "BounceGalleryFlowLayout.h"
#import "BounceGalleryCell.h"
#import "BounceGalleryImageRef.h"
#import "BounceGalleryImageDetail.h"
@interface BounceGalleryViewController ()
{
NSUInteger page;
dispatch_queue_t queue;
dispatch_group_t group;
BounceGalleryImageDetail *detail;
}
@property (nonatomic) NSMutableArray *imgs;
@end
@implementation BounceGalleryViewController
@synthesize imgToScreenAspectRatio = _imgToScreenAspectRatio;
@synthesize imgs = _imgs;
static NSString * const reuseIdentifier = @"Cell";
const CGFloat exit_button_size = 36.0f;
static CGSize CGSizeResizeToHeight(CGSize size, CGFloat height) {
size.width *= height / size.height;
size.height = height;
return size;
}
-(id)initWithCollectionViewLayout:(UICollectionViewLayout *)layout andAssets:(NSArray*)assets
{
if (assets.count == 0) return nil;
self = [self initWithCollectionViewLayout:layout];
if (self)
{
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
group = dispatch_group_create();
page = 0;
_imgToScreenAspectRatio = 4.5f;
_imgs = [NSMutableArray array];
[self initAssets:assets];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self.collectionView registerClass:[BounceGalleryCell class] forCellWithReuseIdentifier:reuseIdentifier];
[self.collectionView setBackgroundColor: [UIColor blackColor]];
[self insertExitButton];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
detail = nil;
}
-(void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
-(void)insertExitButton
{
UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeCustom];
[exitButton setFrame:CGRectMake(exit_button_size*0.25f, exit_button_size*0.25f, exit_button_size, exit_button_size)];
[exitButton setImage:[UIImage imageNamed:@"exit"] forState:UIControlStateNormal];
[exitButton addTarget:self action:@selector(dismissBounceGallery:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:exitButton];
}
-(void)dismissBounceGallery:(id)sender
{
[self dismissViewControllerAnimated:sender completion:nil];
}
-(void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
_imgs = nil;
if (detail) [detail dismissViewControllerAnimated:flag completion:nil];
[super dismissViewControllerAnimated:flag completion:completion];
}
-(void)initAssets:(NSArray*)assetsbatch
{
[self appendImgsWithGalleryImageRefsForAssetsBatch:assetsbatch];
[self constructFramesStartingN:0 onBackgroundThread:FALSE];
}
-(void)addImagesFromAssetsInBatch:(NSArray*)batchpaths
{
NSUInteger n = _imgs.count;
[self appendImgsWithGalleryImageRefsForAssetsBatch:batchpaths];
[self constructFramesStartingN:n onBackgroundThread:TRUE];
}
-(void)insertGalleryImageRefIntImgs:(ALAsset*)asset
{
BounceGalleryImageRef *gi = [BounceGalleryImageRef newWithAsset:asset];
[_imgs insertObject:gi atIndex:0];
}
-(void)appendImgsWithGalleryImageRefsForAssetsBatch:(NSArray*)assetsbatch
{
for (ALAsset *asset in assetsbatch)
{
BounceGalleryImageRef *gi = [BounceGalleryImageRef newWithAsset:asset];
[_imgs addObject:gi];
}
}
-(void)constructFramesStartingN:(NSUInteger)num onBackgroundThread:(BOOL)bg
{
__block NSUInteger n = num;
void (^construct)(void) = ^void(void)
{
CGSize size = self.view.bounds.size;
int N = (int)(_imgs.count - n);
CGSize newSizes[N];
float ideal_height = MAX(size.height, size.width) / _imgToScreenAspectRatio;
float seq[N];
float total_width = 0;
for (int i = 0; i < N; i++)
{
BounceGalleryImageRef *gi = _imgs[i + n];
@autoreleasepool
{
UIImage *image = [gi galleryThumb];
newSizes[i] = CGSizeResizeToHeight(image.size, ideal_height);
}
seq[i] = newSizes[i].width;
total_width += seq[i];
gi = nil;
}
int K = (int)roundf(total_width / size.width);
float M[N][K];
float D[N][K];
for (int i = 0 ; i < N; i++) {
for (int j = 0; j < K; j++) {
D[i][j] = 0;
}
}
for (int i = 0; i < K; i++) {
M[0][i] = seq[0];
}
for (int i = 0; i < N; i++) {
M[i][0] = seq[i] + (i ? M[i-1][0] : 0);
}
float cost;
for (int i = 1; i < N; i++) {
for (int j = 1; j < K; j++) {
M[i][j] = INT_MAX;
for (int k = 0; k < i; k++) {
cost = MAX(M[k][j-1], M[i][0]-M[k][0]);
if (M[i][j] > cost)
{
M[i][j] = cost;
D[i][j] = k;
}
}
}
}
int k1 = K-1;
int n1 = N-1;
int ranges[N][2];
while (k1 >= 0) {
ranges[k1][0] = D[n1][k1]+1;
ranges[k1][1] = n1;
n1 = D[n1][k1];
k1--;
}
ranges[0][0] = 0;
BounceGalleryFlowLayout *layout = (BounceGalleryFlowLayout*) self.collectionViewLayout;
float cellDistance = layout.minimumInteritemSpacing/2;
float widthOffset = cellDistance;
float frameWidth;
for (int i = 0; i < K; i++)
{
float rowWidth = 0;
frameWidth = size.width - (((ranges[i][1] - ranges[i][0]) * 2) * cellDistance);
for (int j = ranges[i][0]; j <= ranges[i][1]; j++) {
rowWidth += newSizes[j].width;
}
float ratio = frameWidth / rowWidth;
widthOffset = 0;
for (int j = ranges[i][0]; j <= ranges[i][1]; j++) {
newSizes[j].width = floorf(ratio * newSizes[j].width);
newSizes[j].height = floorf(ratio * newSizes[j].height);
widthOffset += newSizes[j].width;
}
}
for (int i = 0; i < N; i++)
{
BounceGalleryImageRef *gi = _imgs[i + n];
[gi setSize:newSizes[i]];
}
};
if (!bg)
{
construct();
[self reloadGracefully];
}
else
{
dispatch_group_async(group, queue, ^
{
construct();
dispatch_async(dispatch_get_main_queue(), ^
{
[self reloadGracefully];
});
});
}
}
-(void)reloadGracefully
{
[self.collectionView reloadData];
[self.collectionView performBatchUpdates:^
{
[self.collectionView.collectionViewLayout invalidateLayout];
[self.collectionView setCollectionViewLayout:self.collectionViewLayout animated:YES];
}
completion:nil];
}
-(void)setImgToScreenAspectRatio:(CGFloat)imgToScreenAspectRatio
{
_imgToScreenAspectRatio = imgToScreenAspectRatio;
[self constructFramesStartingN:0 onBackgroundThread:FALSE];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; }
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return _imgs.count; }
- (BounceGalleryCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BounceGalleryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[cell prepareForReuse];
BounceGalleryImageRef *gi = [self gimgForIndexPath:indexPath];
if (gi) [cell setImgRef:gi];
return cell;
}
#pragma mark <UICollectionViewDelegate>
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return [[self gimgForIndexPath:indexPath] size];
}
#pragma mark <UICollectionViewDelegateDataFlow>
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 10.0f;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
}
-(BounceGalleryImageRef*)gimgForIndexPath:(NSIndexPath*)path
{
return _imgs[[path indexAtPosition:[path length] - 1]];
}
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
detail = [[BounceGalleryImageDetail alloc] initWithImageRef:[self gimgForIndexPath:indexPath]];
[self presentViewController:detail animated:TRUE completion:nil];
}
- (BOOL)prefersStatusBarHidden { return TRUE; }
-(BOOL)shouldAutorotate { return FALSE; }
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
@end