2016-05-31 14:50:30 +03:00
|
|
|
//
|
|
|
|
// CKGallery.m
|
|
|
|
// ReactNativeCameraKit
|
|
|
|
//
|
|
|
|
// Created by Ran Greenberg on 30/05/2016.
|
|
|
|
// Copyright © 2016 Wix. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "CKGalleryManager.h"
|
|
|
|
#import <Photos/Photos.h>
|
|
|
|
#import "RCTConvert.h"
|
|
|
|
|
2016-06-07 18:42:59 +03:00
|
|
|
typedef void (^AlbumsBlock)(NSDictionary *albums);
|
|
|
|
|
|
|
|
@interface CKGalleryManager ()
|
|
|
|
|
|
|
|
@property (nonatomic, strong) PHFetchResult *allPhotos;
|
|
|
|
@property (nonatomic, strong) PHFetchResult *smartAlbums;
|
|
|
|
@property (nonatomic, strong) PHFetchResult *topLevelUserCollections;
|
|
|
|
|
|
|
|
@end
|
2016-05-31 14:50:30 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
|
2016-05-31 14:50:30 +03:00
|
|
|
@implementation CKGalleryManager
|
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
|
2016-05-31 14:50:30 +03:00
|
|
|
RCT_EXPORT_MODULE();
|
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
|
2016-06-07 18:42:59 +03:00
|
|
|
-(instancetype)init {
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
[self initAlbums];
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2016-05-31 14:50:30 +03:00
|
|
|
|
2016-06-07 18:42:59 +03:00
|
|
|
-(void)initAlbums {
|
|
|
|
|
|
|
|
PHFetchOptions *allPhotosOptions = [[PHFetchOptions alloc] init];
|
|
|
|
allPhotosOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:YES]];
|
2016-05-31 14:50:30 +03:00
|
|
|
|
2016-06-07 18:42:59 +03:00
|
|
|
PHFetchOptions *albumsOptions = [[PHFetchOptions alloc] init];
|
|
|
|
albumsOptions.predicate = [NSPredicate predicateWithFormat:@"estimatedAssetCount > 0"];
|
2016-05-31 14:50:30 +03:00
|
|
|
|
2016-06-07 18:42:59 +03:00
|
|
|
self.allPhotos = [PHAsset fetchAssetsWithOptions:allPhotosOptions];
|
2016-06-15 14:40:04 +03:00
|
|
|
self.smartAlbums = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeAlbumMyPhotoStream options:nil];
|
2016-06-07 18:42:59 +03:00
|
|
|
self.topLevelUserCollections = [PHCollectionList fetchTopLevelUserCollectionsWithOptions:nil];
|
2016-06-02 14:08:06 +03:00
|
|
|
}
|
|
|
|
|
2016-06-07 18:42:59 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
-(void)extractCollection:(id)collection
|
|
|
|
imageRequestOptions:(PHImageRequestOptions*)options
|
|
|
|
thumbnailSize:(CGSize)thumbnailSize
|
|
|
|
block:(AlbumsBlock)block {
|
2016-05-31 14:50:30 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
NSInteger collectionCount = ([collection isKindOfClass:[PHAssetCollection class]]) ? [PHAsset fetchAssetsInAssetCollection:collection options:nil].count : ((PHFetchResult*)collection).count;
|
2016-06-02 14:08:06 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
if (collectionCount > 0){
|
2016-06-14 18:12:50 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
NSString *albumName = ([collection isKindOfClass:[PHAssetCollection class]]) ? ((PHAssetCollection*)collection).localizedTitle : @"All photos";
|
|
|
|
PHFetchResult *fetchResult = ([collection isKindOfClass:[PHAssetCollection class]]) ? [PHAsset fetchKeyAssetsInAssetCollection:collection options:nil] : (PHAssetCollection*)collection;
|
|
|
|
PHAsset *thumbnail = [fetchResult firstObject];
|
2016-06-14 18:12:50 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
NSMutableDictionary *albumInfo = [[NSMutableDictionary alloc] init];
|
|
|
|
albumInfo[@"albumName"] = albumName;
|
|
|
|
albumInfo[@"imagesCount"] = [NSNumber numberWithInteger:collectionCount];
|
2016-06-14 18:12:50 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
[[PHImageManager defaultManager]
|
|
|
|
requestImageForAsset:thumbnail
|
|
|
|
targetSize:thumbnailSize
|
|
|
|
contentMode:PHImageContentModeAspectFit
|
|
|
|
options:options
|
|
|
|
resultHandler:^(UIImage *result, NSDictionary *info) {
|
|
|
|
|
|
|
|
if (!albumInfo[@"image"]) {
|
|
|
|
albumInfo[@"image"] = [UIImageJPEGRepresentation(result, 1.0) base64Encoding];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (block) {
|
|
|
|
block(albumInfo);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}
|
2016-06-22 18:28:24 +03:00
|
|
|
|
|
|
|
else {
|
|
|
|
if (block) {
|
|
|
|
block(nil);
|
|
|
|
}
|
|
|
|
}
|
2016-06-07 18:42:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
-(void)extractCollectionsDetails:(PHFetchResult*)collections
|
|
|
|
imageRequestOptions:(PHImageRequestOptions*)options
|
|
|
|
thumbnailSize:(CGSize)thumbnailSize
|
|
|
|
block:(AlbumsBlock)block {
|
2016-06-14 18:12:50 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
NSMutableDictionary *albumsDict = [[NSMutableDictionary alloc] init];
|
|
|
|
NSInteger collectionCount = collections.count;
|
|
|
|
|
|
|
|
[collections enumerateObjectsUsingBlock:^(PHAssetCollection *collection, NSUInteger idx, BOOL * _Nonnull stop) {
|
2016-06-02 14:08:06 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
[self extractCollection:collection imageRequestOptions:options thumbnailSize:thumbnailSize block:^(NSDictionary *album) {
|
2016-06-02 14:08:06 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
NSString *albumName = collection.localizedTitle;
|
|
|
|
if (album) {
|
|
|
|
albumsDict[albumName] = album;
|
|
|
|
}
|
2016-06-02 14:08:06 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
if (idx == collectionCount-1) {
|
|
|
|
if (block) {
|
|
|
|
block(albumsDict);
|
|
|
|
}
|
2016-06-07 18:42:59 +03:00
|
|
|
}
|
2016-06-15 14:40:04 +03:00
|
|
|
}];
|
2016-05-31 14:50:30 +03:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2016-06-14 18:12:50 +03:00
|
|
|
|
|
|
|
RCT_EXPORT_METHOD(getAlbumsWithThumbnails:(RCTPromiseResolveBlock)resolve
|
2016-06-15 14:40:04 +03:00
|
|
|
reject:(__unused RCTPromiseRejectBlock)reject) {
|
2016-06-14 18:12:50 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
PHImageRequestOptions *imageRequestOptions = [[PHImageRequestOptions alloc] init];
|
|
|
|
imageRequestOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
|
|
|
|
imageRequestOptions.synchronous = YES;
|
|
|
|
NSInteger retinaScale = [UIScreen mainScreen].scale;
|
|
|
|
CGSize retinaSquare = CGSizeMake(100*retinaScale, 100*retinaScale);
|
2016-06-14 18:12:50 +03:00
|
|
|
|
2016-06-15 14:40:04 +03:00
|
|
|
NSMutableDictionary *albumsDict = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
|
|
[self extractCollectionsDetails:self.topLevelUserCollections
|
|
|
|
imageRequestOptions:imageRequestOptions
|
|
|
|
thumbnailSize:retinaSquare
|
|
|
|
block:^(NSDictionary *albums) {
|
|
|
|
|
|
|
|
if (albums) {
|
|
|
|
[albumsDict addEntriesFromDictionary:albums];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self extractCollection:self.allPhotos imageRequestOptions:imageRequestOptions thumbnailSize:retinaSquare block:^(NSDictionary *album) {
|
|
|
|
|
|
|
|
if (album) {
|
|
|
|
albumsDict[album[@"albumName"]] = album;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resolve) {
|
|
|
|
NSDictionary *ans = @{[NSString stringWithFormat:@"albums"]: albumsDict};
|
|
|
|
resolve(ans);
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
}];
|
2016-06-14 18:12:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-05-31 14:50:30 +03:00
|
|
|
@end
|