When I used PhotoKit to create my albums,there is not all albums in my project,some albums imported from computer can't be searched.I will post my code follow.
//get system albums
PHImageRequestOptions * options = [[PHImageRequestOptions alloc] init];
options.networkAccessAllowed = YES;
options.synchronous = YES;
KVPhotoAlbum * userAlum = nil;
PHCachingImageManager * manager = [[PHCachingImageManager alloc] init];
PHFetchResult * systemAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
for (id object in systemAlbums) {
if ([object isKindOfClass:[PHAssetCollection class]]) {
PHAssetCollection * collection = (PHAssetCollection*)object;
if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumVideos) {
continue;
}
//get photo
PHFetchResult * imageResults = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
if (imageResults.count) {
KVPhotoAlbum * album = [[KVPhotoAlbum alloc] init];
album.collection = collection;
album.title = [collection valueForKey:@"localizedTitle"];
album.photoCount = imageResults.count;
if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumUserLibrary) {
//camera
userAlum = album;
}else {
[_albums addObject:album];
}
PHAsset * asset = [imageResults lastObject];
[manager requestImageForAsset:asset targetSize:CGSizeMake(60, 60) contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
album.image = result;
}];
}
}
}
//get albums created by user
PHFetchResult * customAlbums = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
for (id object in customAlbums) {
if ([object isKindOfClass:[PHAssetCollection class]]) {
PHAssetCollection * collection = (PHAssetCollection*)object;
if (collection.assetCollectionSubtype == PHAssetCollectionSubtypeSmartAlbumVideos) {
continue;
}
//get photo
PHFetchResult * imageResults = [PHAsset fetchAssetsInAssetCollection:collection options:nil];
if (imageResults.count) {
KVPhotoAlbum * album = [[KVPhotoAlbum alloc] init];
album.collection = collection;
album.title = [collection valueForKey:@"localizedTitle"];
album.photoCount = imageResults.count;
[_albums addObject:album];
PHAsset * asset = [imageResults lastObject];
[manager requestImageForAsset:asset targetSize:CGSizeMake(60, 60) contentMode:PHImageContentModeDefault options:options resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
album.image = result;
}];
}
}
}
Too many code,I have to add more details.
I solved it by myself,now I show the way. There is a album type,"PHAssetCollectionTypeAlbum",this can search these albums imported from computer.