ZLPhotoTool.m
7.36 KB
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
//
// ZLPhotoTool.m
// 多选相册照片
//
// Created by long on 15/11/30.
// Copyright © 2015年 long. All rights reserved.
//
#import "ZLPhotoTool.h"
@implementation ZLPhotoAblumList
@end
@implementation ZLPhotoTool
static ZLPhotoTool *sharePhotoTool = nil;
+ (instancetype)sharePhotoTool
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharePhotoTool = [[self alloc] init];
});
return sharePhotoTool;
}
+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharePhotoTool = [super allocWithZone:zone];
});
return sharePhotoTool;
}
#pragma mark - 获取所有相册列表
- (NSArray<ZLPhotoAblumList *> *)getPhotoAblumList
{
NSMutableArray<ZLPhotoAblumList *> *photoAblumList = [NSMutableArray array];
// ZLPhotoAblumList *ablum = [[ZLPhotoAblumList alloc] init];
// ablum.title = [self transformAblumTitle:collection.localizedTitle];
// ablum.count = assets.count;
// ablum.headImageAsset = assets.firstObject;
// ablum.assetCollection = collection;
// [photoAblumList addObject:ablum];
// 列出所有相册智能相册
PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
// 列出所有用户创建的相册
PHFetchResult *topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
// //获取所有智能相册
// PHFetchResult *smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
[smartAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL *stop) {
//过滤掉视频和最近删除
if (!([collection.localizedTitle isEqualToString:@"Recently Deleted"] ||
[collection.localizedTitle isEqualToString:@"Videos"])) {
NSArray<PHAsset *> *assets = [self getAssetsInAssetCollection:collection ascending:NO];
if (assets.count > 0) {
ZLPhotoAblumList *ablum = [[ZLPhotoAblumList alloc] init];
ablum.title = [self transformAblumTitle:collection.localizedTitle];
ablum.count = assets.count;
ablum.headImageAsset = assets.firstObject;
ablum.assetCollection = collection;
[photoAblumList addObject:ablum];
}
}
}];
//
// //获取用户创建的相册
// PHFetchResult *userAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil];
// [userAlbums enumerateObjectsUsingBlock:^(PHAssetCollection * _Nonnull collection, NSUInteger idx, BOOL * _Nonnull stop) {
// NSArray<PHAsset *> *assets = [self getAssetsInAssetCollection:collection ascending:NO];
// if (assets.count > 0) {
// ZLPhotoAblumList *ablum = [[ZLPhotoAblumList alloc] init];
// ablum.title = collection.localizedTitle;
// ablum.count = assets.count;
// ablum.headImageAsset = assets.firstObject;
// ablum.assetCollection = collection;
// [photoAblumList addObject:ablum];
// }
// }];
return photoAblumList;
}
- (NSString *)transformAblumTitle:(NSString *)title
{
if ([title isEqualToString:@"Slo-mo"]) {
return @"慢动作";
} else if ([title isEqualToString:@"Recently Added"]) {
return @"最近添加";
} else if ([title isEqualToString:@"Favorites"]) {
return @"最爱";
} else if ([title isEqualToString:@"Recently Deleted"]) {
return @"最近删除";
} else if ([title isEqualToString:@"Videos"]) {
return @"视频";
} else if ([title isEqualToString:@"All Photos"]) {
return @"所有照片";
} else if ([title isEqualToString:@"Selfies"]) {
return @"自拍";
} else if ([title isEqualToString:@"Screenshots"]) {
return @"屏幕快照";
} else if ([title isEqualToString:@"Camera Roll"]) {
return @"相机胶卷";
} else if ([title isEqualToString:@"Panoramas"]) {
return @"全景照片";
}
return nil;
}
- (PHFetchResult *)fetchAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
PHFetchOptions *option = [[PHFetchOptions alloc] init];
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];
PHFetchResult *result = [PHAsset fetchAssetsInAssetCollection:assetCollection options:option];
return result;
}
#pragma mark - 获取相册内所有照片资源
- (NSArray<PHAsset *> *)getAllAssetInPhotoAblumWithAscending:(BOOL)ascending
{
NSMutableArray<PHAsset *> *assets = [NSMutableArray array];
PHFetchOptions *option = [[PHFetchOptions alloc] init];
//ascending 为YES时,按照照片的创建时间升序排列;为NO时,则降序排列
option.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:ascending]];
PHFetchResult *result = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:option];
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
PHAsset *asset = (PHAsset *)obj;
[assets addObject:asset];
}];
return assets;
}
#pragma mark - 获取指定相册内的所有图片
- (NSArray<PHAsset *> *)getAssetsInAssetCollection:(PHAssetCollection *)assetCollection ascending:(BOOL)ascending
{
NSMutableArray<PHAsset *> *arr = [NSMutableArray array];
PHFetchResult *result = [self fetchAssetsInAssetCollection:assetCollection ascending:ascending];
[result enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
if (((PHAsset *)obj).mediaType == PHAssetMediaTypeImage) {
[arr addObject:obj];
}
}];
return arr;
}
#pragma mark - 获取asset对应的图片
- (void)requestImageForAsset:(PHAsset *)asset size:(CGSize)size resizeMode:(PHImageRequestOptionsResizeMode)resizeMode completion:(void (^)(UIImage *))completion
{
PHImageRequestOptions *option = [[PHImageRequestOptions alloc] init];
/**
resizeMode:对请求的图像怎样缩放。有三种选择:None,默认加载方式;Fast,尽快地提供接近或稍微大于要求的尺寸;Exact,精准提供要求的尺寸。
deliveryMode:图像质量。有三种值:Opportunistic,在速度与质量中均衡;HighQualityFormat,不管花费多长时间,提供高质量图像;FastFormat,以最快速度提供好的质量。
这个属性只有在 synchronous 为 true 时有效。
*/
option.resizeMode = resizeMode;//控制照片尺寸
option.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;//控制照片质量
//option.synchronous = YES;
option.networkAccessAllowed = YES;
//param:targetSize 即你想要的图片尺寸,若想要原尺寸则可输入PHImageManagerMaximumSize
[[PHCachingImageManager defaultManager] requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFit options:option resultHandler:^(UIImage * _Nullable image, NSDictionary * _Nullable info) {
completion(image);
}];
}
@end