-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathViewController.m
82 lines (64 loc) · 3.55 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
//
// 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"
#import "PHCollection.h" // Currently seems `PHAssetCollection` is not list in public header, but it works
static NSString * const TestCollectionViewItemIdentifier = @"TestCollectionViewItemIdentifier";
@interface ViewController () <NSCollectionViewDelegate, NSCollectionViewDataSource>
@property (nonatomic, strong) NSMutableArray<NSURL *> *objects;
@property (weak) IBOutlet NSScrollView *scrollView;
@property (weak) IBOutlet NSCollectionView *collectionView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.objects = [NSMutableArray array];
// Setup Photos Loader
SDWebImageManager.defaultImageLoader = [SDWebImagePhotosLoader 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 :)
SDWebImagePhotosLoader.sharedLoader.imageRequestOptions = options;
// Photos Library Demo
PHFetchResult<PHAssetCollection *> *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum
subtype:PHAssetCollectionSubtypeAlbumImported
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];
}
[self.collectionView registerClass:[TestCollectionViewItem class] forItemWithIdentifier:TestCollectionViewItemIdentifier];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {
TestCollectionViewItem *cell = [collectionView makeItemWithIdentifier:TestCollectionViewItemIdentifier 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