-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathViewController.m
97 lines (80 loc) · 3.8 KB
/
ViewController.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
//
// ViewController.m
// SDWebImagePhotosPlugin_Example macOS
//
// Created by lizhuoli on 2018/7/19.
// Copyright © 2018年 DreamPiggy. All rights reserved.
//
#import "ViewController.h"
#import <SDWebImagePhotosPlugin/SDWebImagePhotosPlugin.h>
#import "TestCollectionViewItem.h"
@interface ViewController () <NSCollectionViewDelegate, NSCollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray<NSURL *> *objects;
@property (weak) IBOutlet NSScrollView *scrollView;
@property (weak) IBOutlet NSCollectionView *collectionView;
@end
@implementation ViewController
- (instancetype)initWithCoder:(NSCoder *)coder {
self = [super initWithCoder:coder];
if (self) {
self.objects = [NSMutableArray array];
// Setup Photos Loader
SDWebImageManager.defaultImageLoader = [SDImagePhotosLoader sharedLoader];
PHImageRequestOptions *options = [PHImageRequestOptions new];
options.sd_targetSize = CGSizeMake(500, 500); // The original image size may be 4K, we only query the max view size :)
SDImagePhotosLoader.sharedLoader.imageRequestOptions = options;
// Reload
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(menuItemDidTap:) name:NSMenuDidSendActionNotification object:nil];
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Photos Library Demo
[self reloadData];
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunguarded-availability"
- (void)fetchAssets {
[self.objects removeAllObjects];
PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum
subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary
options:nil];
PHAssetCollection *collection = result.firstObject;
PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType = %d", PHAssetMediaTypeImage];
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult<PHAsset *> *assets = [PHAsset fetchAssetsInAssetCollection:collection options:fetchOptions];
for (PHAsset *asset in assets) {
// You can use local identifier of `PHAsset` to create URL
// NSURL *url = [NSURL sd_URLWithAssetLocalIdentifier:asset.localIdentifier];
// Or even `PHAsset` itself
NSURL *url = [NSURL sd_URLWithAsset:asset];
[self.objects addObject:url];
}
}
#pragma clang diagnostic pop
- (void)menuItemDidTap:(NSNotification *)notification {
NSMenuItem *menuItem = notification.userInfo[@"MenuItem"];
if ([menuItem.title isEqualToString:@"Reload"]) {
[self reloadData];
}
}
- (void)reloadData {
[self fetchAssets];
[self.collectionView reloadData];
}
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {
TestCollectionViewItem *cell = [collectionView makeItemWithIdentifier:@"TestCollectionViewItem" forIndexPath:indexPath];
NSURL *photosURL = self.objects[indexPath.item];
cell.imageViewDisplay.sd_imageTransition = SDWebImageTransition.fadeTransition;
[cell.imageViewDisplay sd_setImageWithURL:photosURL placeholderImage:nil options:SDWebImageFromLoaderOnly context:@{SDWebImageContextStoreCacheType: @(SDImageCacheTypeNone)}];
return cell;
}
- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return self.objects.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView {
return 1;
}
@end