11
11
#import " TestCollectionViewItem.h"
12
12
#import " PHCollection.h" // Currently seems `PHAssetCollection` is not list in public header, but it works
13
13
14
- static NSString * const TestCollectionViewItemIdentifier = @" TestCollectionViewItemIdentifier" ;
15
-
16
14
@interface ViewController () <NSCollectionViewDelegate , NSCollectionViewDataSource >
17
15
18
16
@property (nonatomic , strong ) NSMutableArray <NSURL *> *objects;
@@ -24,19 +22,33 @@ @interface ViewController () <NSCollectionViewDelegate, NSCollectionViewDataSour
24
22
25
23
@implementation ViewController
26
24
25
+ - (instancetype )initWithCoder : (NSCoder *)coder {
26
+ self = [super initWithCoder: coder];
27
+ if (self) {
28
+ self.objects = [NSMutableArray array ];
29
+ // Setup Photos Loader
30
+ SDWebImageManager.defaultImageLoader = [SDWebImagePhotosLoader sharedLoader ];
31
+ PHImageRequestOptions *options = [PHImageRequestOptions new ];
32
+ options.sd_targetSize = CGSizeMake (500 , 500 ); // The original image size may be 4K, we only query the max view size :)
33
+ SDWebImagePhotosLoader.sharedLoader .imageRequestOptions = options;
34
+
35
+ // Reload
36
+ [[NSNotificationCenter defaultCenter ] addObserver: self selector: @selector (menuItemDidTap: ) name: NSMenuDidSendActionNotification object: nil ];
37
+ }
38
+ return self;
39
+ }
40
+
27
41
- (void )viewDidLoad {
28
42
[super viewDidLoad ];
29
43
30
- self.objects = [NSMutableArray array ];
31
- // Setup Photos Loader
32
- SDWebImageManager.defaultImageLoader = [SDWebImagePhotosLoader sharedLoader ];
33
- PHImageRequestOptions *options = [PHImageRequestOptions new ];
34
- options.sd_targetSize = CGSizeMake (500 , 500 ); // The original image size may be 4K, we only query the max view size :)
35
- SDWebImagePhotosLoader.sharedLoader .imageRequestOptions = options;
36
-
37
44
// Photos Library Demo
38
- PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithType: PHAssetCollectionTypeAlbum
39
- subtype: PHAssetCollectionSubtypeAlbumImported
45
+ [self reloadData ];
46
+ }
47
+
48
+ - (void )fetchAssets {
49
+ [self .objects removeAllObjects ];
50
+ PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithType: PHAssetCollectionTypeSmartAlbum
51
+ subtype: PHAssetCollectionSubtypeSmartAlbumUserLibrary
40
52
options: nil ];
41
53
PHAssetCollection *collection = result.firstObject ;
42
54
PHFetchOptions *fetchOptions = [PHFetchOptions new ];
@@ -50,21 +62,22 @@ - (void)viewDidLoad {
50
62
NSURL *url = [NSURL sd_URLWithAsset: asset];
51
63
[self .objects addObject: url];
52
64
}
53
-
54
- [self .collectionView registerClass: [TestCollectionViewItem class ] forItemWithIdentifier: TestCollectionViewItemIdentifier];
55
- self.collectionView .delegate = self;
56
- self.collectionView .dataSource = self;
57
65
}
58
66
67
+ - (void )menuItemDidTap : (NSNotification *)notification {
68
+ NSMenuItem *menuItem = notification.userInfo [@" MenuItem" ];
69
+ if ([menuItem.title isEqualToString: @" Reload" ]) {
70
+ [self reloadData ];
71
+ }
72
+ }
59
73
60
- - (void )setRepresentedObject : (id )representedObject {
61
- [super setRepresentedObject: representedObject];
62
-
63
- // Update the view, if already loaded.
74
+ - (void )reloadData {
75
+ [self fetchAssets ];
76
+ [self .collectionView reloadData ];
64
77
}
65
78
66
79
- (NSCollectionViewItem *)collectionView : (NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath : (NSIndexPath *)indexPath {
67
- TestCollectionViewItem *cell = [collectionView makeItemWithIdentifier: TestCollectionViewItemIdentifier forIndexPath: indexPath];
80
+ TestCollectionViewItem *cell = [collectionView makeItemWithIdentifier: @" TestCollectionViewItem " forIndexPath: indexPath];
68
81
NSURL *photosURL = self.objects [indexPath.item];
69
82
cell.imageViewDisplay .sd_imageTransition = SDWebImageTransition.fadeTransition ;
70
83
[cell.imageViewDisplay sd_setImageWithURL: photosURL placeholderImage: nil options: SDWebImageFromLoaderOnly context: @{SDWebImageContextStoreCacheType: @(SDImageCacheTypeNone)}];
0 commit comments