11#import " DemoViewController.h"
2- #import < WPMediaPicker/WPMediaPickerViewController.h>
2+ #import " WPPHAssetDataSource.h"
3+ #import " OptionsViewController.h"
4+ #import < WPMediaPicker/WPMediaPicker.h>
5+ #import < WPMediaPicker/WPMediaGroupTableViewCell.h>
6+
7+ @interface DemoViewController () <WPMediaPickerViewControllerDelegate, OptionsViewControllerDelegate>
38
4- @interface DemoViewController () <WPMediaPickerViewControllerDelegate>
59@property (nonatomic , strong ) NSArray * assets;
610@property (nonatomic , strong ) NSDateFormatter * dateFormatter;
11+ @property (nonatomic , strong ) id <WPMediaCollectionDataSource> customDataSource;
12+ @property (nonatomic , copy ) NSDictionary *options;
13+
714@end
815
916@implementation DemoViewController
@@ -14,14 +21,21 @@ - (void)viewDidLoad
1421
1522 self.title = @" WPMediaPicker" ;
1623 // setup nav buttons
17- self.navigationItem .leftBarButtonItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemTrash target: self action: @selector (clearSelection : )];
24+ self.navigationItem .leftBarButtonItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemAction target: self action: @selector (showOptions : )];
1825
1926 self.navigationItem .rightBarButtonItem = [[UIBarButtonItem alloc ] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target: self action: @selector (showPicker: )];
2027
2128 // date formatter
2229 self.dateFormatter = [[NSDateFormatter alloc ] init ];
2330 self.dateFormatter .dateStyle = NSDateFormatterMediumStyle;
2431 self.dateFormatter .timeStyle = NSDateFormatterMediumStyle;
32+ [self .tableView registerClass: [WPMediaGroupTableViewCell class ] forCellReuseIdentifier: NSStringFromClass ([WPMediaGroupTableViewCell class ])];
33+ self.options = @{
34+ MediaPickerOptionsShowMostRecentFirst:@(NO ),
35+ MediaPickerOptionsUsePhotosLibrary:@(NO ),
36+ MediaPickerOptionsShowCameraCapture:@(YES )
37+ };
38+
2539}
2640
2741- (void )didReceiveMemoryWarning
@@ -44,19 +58,30 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger
4458
4559- (UITableViewCell *) tableView : (UITableView *)tableView cellForRowAtIndexPath : (NSIndexPath *)indexPath
4660{
47- UITableViewCell * cell = [self .tableView dequeueReusableCellWithIdentifier: @" Cell " ];
61+ WPMediaGroupTableViewCell * cell = [self .tableView dequeueReusableCellWithIdentifier: NSStringFromClass ([WPMediaGroupTableViewCell class ]) forIndexPath: indexPath ];
4862
49- if (!cell){
50- cell = [[UITableViewCell alloc ] initWithStyle: UITableViewCellStyleSubtitle reuseIdentifier: @" Cell" ];
63+ id <WPMediaAsset> asset = self.assets [indexPath.row];
64+ __block WPMediaRequestID requestID = 0 ;
65+ requestID = [asset imageWithSize: CGSizeMake (100 ,100 ) completionHandler: ^(UIImage *result, NSError *error) {
66+ if (error) {
67+ return ;
68+ }
69+ if (cell.tag == requestID) {
70+ dispatch_async (dispatch_get_main_queue (), ^{
71+ cell.imagePosterView .image = result;
72+ });
73+ }
74+ }];
75+ cell.tag = requestID;
76+ cell.titleLabel .text = [self .dateFormatter stringFromDate: [asset date ]];
77+ if ([asset assetType ] == WPMediaTypeImage) {
78+ cell.countLabel .text = @" Image" ;
79+ } else if ([asset assetType ] == WPMediaTypeVideo) {
80+ cell.countLabel .text = @" Video" ;
81+ } else {
82+ cell.countLabel .text = @" Other" ;
5183 }
5284
53- ALAsset * asset = self.assets [indexPath.row];
54- cell.imageView .image = [UIImage imageWithCGImage: [asset thumbnail ]];
55-
56- cell.textLabel .text = [self .dateFormatter stringFromDate: [asset valueForProperty: ALAssetPropertyDate]];
57- cell.detailTextLabel .text = [asset valueForProperty: ALAssetPropertyType];
58- cell.detailTextLabel .hidden = NO ;
59-
6085 return cell;
6186}
6287
@@ -86,9 +111,36 @@ - (void) clearSelection:(id) sender
86111
87112- (void ) showPicker : (id ) sender
88113{
89- WPMediaPickerViewController * mediaPicker = [[WPMediaPickerViewController alloc ] init ];
114+ WPMediaPickerViewController *mediaPicker = [[WPMediaPickerViewController alloc ] init ];
90115 mediaPicker.delegate = self;
116+ mediaPicker.showMostRecentFirst = [self .options[MediaPickerOptionsShowMostRecentFirst] boolValue ];
117+ if ([self .options[MediaPickerOptionsUsePhotosLibrary] boolValue ]){
118+ self.customDataSource = [[WPPHAssetDataSource alloc ] init ];
119+ mediaPicker.dataSource = self.customDataSource ;
120+ }
121+ mediaPicker.allowCaptureOfMedia = [self .options[MediaPickerOptionsShowCameraCapture] boolValue ];
91122 [self presentViewController: mediaPicker animated: YES completion: nil ];
92123}
93124
125+ - (void ) showOptions : (id ) sender
126+ {
127+ OptionsViewController *optionsViewController = [[OptionsViewController alloc ] init ];
128+ optionsViewController.delegate = self;
129+ optionsViewController.options = self.options ;
130+ [[self navigationController ] pushViewController: optionsViewController animated: YES ];
131+ }
132+
133+ #pragma - Options
134+
135+ - (void )optionsViewController : (OptionsViewController *)optionsViewController changed : (NSDictionary *)options
136+ {
137+ self.options = options;
138+ [self .navigationController popViewControllerAnimated: YES ];
139+ }
140+
141+ - (void )cancelOptionsViewController : (OptionsViewController *)optionsViewController
142+ {
143+ [self .navigationController popViewControllerAnimated: YES ];
144+ }
145+
94146@end
0 commit comments