mirror of
https://github.com/status-im/react-native-cameraroll.git
synced 2025-01-14 11:54:39 +00:00
AssetsLibrary.framework --> Photos.framework; ReactNative stuff
Summary: AssetsLibrary.framework --> Photos.framework; ReactNative Port RN off of AssetsLibrary where trivial, lazy load AssetsLibrary where remaining. Reviewed By: fkgozali Differential Revision: D7999924 fbshipit-source-id: 1d48a2d820984564459ed43680cd3f601399d352
This commit is contained in:
parent
53ea464b2d
commit
366c80eab5
@ -8,6 +8,8 @@
|
|||||||
#import "RCTAssetsLibraryRequestHandler.h"
|
#import "RCTAssetsLibraryRequestHandler.h"
|
||||||
|
|
||||||
#import <stdatomic.h>
|
#import <stdatomic.h>
|
||||||
|
#import <dlfcn.h>
|
||||||
|
#import <objc/runtime.h>
|
||||||
|
|
||||||
#import <AssetsLibrary/AssetsLibrary.h>
|
#import <AssetsLibrary/AssetsLibrary.h>
|
||||||
#import <MobileCoreServices/MobileCoreServices.h>
|
#import <MobileCoreServices/MobileCoreServices.h>
|
||||||
@ -23,10 +25,20 @@
|
|||||||
RCT_EXPORT_MODULE()
|
RCT_EXPORT_MODULE()
|
||||||
|
|
||||||
@synthesize bridge = _bridge;
|
@synthesize bridge = _bridge;
|
||||||
|
static Class _ALAssetsLibrary = nil;
|
||||||
|
static void ensureAssetsLibLoaded(void)
|
||||||
|
{
|
||||||
|
static dispatch_once_t onceToken;
|
||||||
|
dispatch_once(&onceToken, ^{
|
||||||
|
void * handle = dlopen("/System/Library/Frameworks/AssetsLibrary.framework/AssetsLibrary", RTLD_LAZY);
|
||||||
|
#pragma unused(handle)
|
||||||
|
_ALAssetsLibrary = objc_getClass("ALAssetsLibrary");
|
||||||
|
});
|
||||||
|
}
|
||||||
- (ALAssetsLibrary *)assetsLibrary
|
- (ALAssetsLibrary *)assetsLibrary
|
||||||
{
|
{
|
||||||
return _assetsLibrary ?: (_assetsLibrary = [ALAssetsLibrary new]);
|
ensureAssetsLibLoaded();
|
||||||
|
return _assetsLibrary ?: (_assetsLibrary = [_ALAssetsLibrary new]);
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark - RCTURLRequestHandler
|
#pragma mark - RCTURLRequestHandler
|
||||||
|
@ -11,6 +11,8 @@
|
|||||||
#import <Foundation/Foundation.h>
|
#import <Foundation/Foundation.h>
|
||||||
#import <UIKit/UIKit.h>
|
#import <UIKit/UIKit.h>
|
||||||
#import <Photos/Photos.h>
|
#import <Photos/Photos.h>
|
||||||
|
#import <dlfcn.h>
|
||||||
|
#import <objc/runtime.h>
|
||||||
|
|
||||||
#import <React/RCTBridge.h>
|
#import <React/RCTBridge.h>
|
||||||
#import <React/RCTConvert.h>
|
#import <React/RCTConvert.h>
|
||||||
@ -44,22 +46,52 @@ RCT_ENUM_CONVERTER(ALAssetsGroupType, (@{
|
|||||||
|
|
||||||
}), ALAssetsGroupSavedPhotos, integerValue)
|
}), ALAssetsGroupSavedPhotos, integerValue)
|
||||||
|
|
||||||
|
static Class _ALAssetsFilter = nil;
|
||||||
|
static NSString *_ALAssetsGroupPropertyName = nil;
|
||||||
|
static NSString *_ALAssetPropertyAssetURL = nil;
|
||||||
|
static NSString *_ALAssetPropertyLocation = nil;
|
||||||
|
static NSString *_ALAssetPropertyDate = nil;
|
||||||
|
static NSString *_ALAssetPropertyType = nil;
|
||||||
|
static NSString *_ALAssetPropertyDuration = nil;
|
||||||
|
static NSString *_ALAssetTypeVideo = nil;
|
||||||
|
static NSString *lookupNSString(void * handle, const char * name)
|
||||||
|
{
|
||||||
|
void ** sym = dlsym(handle, name);
|
||||||
|
return (__bridge NSString *)(sym ? *sym : nil);
|
||||||
|
}
|
||||||
|
static void ensureAssetsLibLoaded(void)
|
||||||
|
{
|
||||||
|
static dispatch_once_t onceToken;
|
||||||
|
dispatch_once(&onceToken, ^{
|
||||||
|
void * handle = dlopen("/System/Library/Frameworks/AssetsLibrary.framework/AssetsLibrary", RTLD_LAZY);
|
||||||
|
RCTAssert(handle != NULL, @"Unable to load AssetsLibrary.framework.");
|
||||||
|
_ALAssetsFilter = objc_getClass("ALAssetsFilter");
|
||||||
|
_ALAssetsGroupPropertyName = lookupNSString(handle, "ALAssetsGroupPropertyName");
|
||||||
|
_ALAssetPropertyAssetURL = lookupNSString(handle, "ALAssetPropertyAssetURL");
|
||||||
|
_ALAssetPropertyLocation = lookupNSString(handle, "ALAssetPropertyLocation");
|
||||||
|
_ALAssetPropertyDate = lookupNSString(handle, "ALAssetPropertyDate");
|
||||||
|
_ALAssetPropertyType = lookupNSString(handle, "ALAssetPropertyType");
|
||||||
|
_ALAssetPropertyDuration = lookupNSString(handle, "ALAssetPropertyDuration");
|
||||||
|
_ALAssetTypeVideo = lookupNSString(handle, "ALAssetTypeVideo");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
+ (ALAssetsFilter *)ALAssetsFilter:(id)json
|
+ (ALAssetsFilter *)ALAssetsFilter:(id)json
|
||||||
{
|
{
|
||||||
static NSDictionary<NSString *, ALAssetsFilter *> *options;
|
static NSDictionary<NSString *, ALAssetsFilter *> *options;
|
||||||
static dispatch_once_t onceToken;
|
static dispatch_once_t onceToken;
|
||||||
dispatch_once(&onceToken, ^{
|
dispatch_once(&onceToken, ^{
|
||||||
|
ensureAssetsLibLoaded();
|
||||||
options = @{
|
options = @{
|
||||||
|
|
||||||
// New values
|
// New values
|
||||||
@"photos": [ALAssetsFilter allPhotos],
|
@"photos": [_ALAssetsFilter allPhotos],
|
||||||
@"videos": [ALAssetsFilter allVideos],
|
@"videos": [_ALAssetsFilter allVideos],
|
||||||
@"all": [ALAssetsFilter allAssets],
|
@"all": [_ALAssetsFilter allAssets],
|
||||||
|
|
||||||
// Legacy values
|
// Legacy values
|
||||||
@"Photos": [ALAssetsFilter allPhotos],
|
@"Photos": [_ALAssetsFilter allPhotos],
|
||||||
@"Videos": [ALAssetsFilter allVideos],
|
@"Videos": [_ALAssetsFilter allVideos],
|
||||||
@"All": [ALAssetsFilter allAssets],
|
@"All": [_ALAssetsFilter allAssets],
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -68,7 +100,7 @@ RCT_ENUM_CONVERTER(ALAssetsGroupType, (@{
|
|||||||
RCTLogError(@"Invalid filter option: '%@'. Expected one of 'photos',"
|
RCTLogError(@"Invalid filter option: '%@'. Expected one of 'photos',"
|
||||||
"'videos' or 'all'.", json);
|
"'videos' or 'all'.", json);
|
||||||
}
|
}
|
||||||
return filter ?: [ALAssetsFilter allPhotos];
|
return filter ?: [_ALAssetsFilter allPhotos];
|
||||||
}
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
@ -149,6 +181,7 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
|
|||||||
{
|
{
|
||||||
checkPhotoLibraryConfig();
|
checkPhotoLibraryConfig();
|
||||||
|
|
||||||
|
ensureAssetsLibLoaded();
|
||||||
NSUInteger first = [RCTConvert NSInteger:params[@"first"]];
|
NSUInteger first = [RCTConvert NSInteger:params[@"first"]];
|
||||||
NSString *afterCursor = [RCTConvert NSString:params[@"after"]];
|
NSString *afterCursor = [RCTConvert NSString:params[@"after"]];
|
||||||
NSString *groupName = [RCTConvert NSString:params[@"groupName"]];
|
NSString *groupName = [RCTConvert NSString:params[@"groupName"]];
|
||||||
@ -161,12 +194,12 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
|
|||||||
NSMutableArray<NSDictionary<NSString *, id> *> *assets = [NSMutableArray new];
|
NSMutableArray<NSDictionary<NSString *, id> *> *assets = [NSMutableArray new];
|
||||||
|
|
||||||
[_bridge.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stopGroups) {
|
[_bridge.assetsLibrary enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stopGroups) {
|
||||||
if (group && (groupName == nil || [groupName isEqualToString:[group valueForProperty:ALAssetsGroupPropertyName]])) {
|
if (group && (groupName == nil || [groupName isEqualToString:[group valueForProperty:_ALAssetsGroupPropertyName]])) {
|
||||||
|
|
||||||
[group setAssetsFilter:assetType];
|
[group setAssetsFilter:assetType];
|
||||||
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stopAssets) {
|
[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stopAssets) {
|
||||||
if (result) {
|
if (result) {
|
||||||
NSString *uri = ((NSURL *)[result valueForProperty:ALAssetPropertyAssetURL]).absoluteString;
|
NSString *uri = ((NSURL *)[result valueForProperty:_ALAssetPropertyAssetURL]).absoluteString;
|
||||||
if (afterCursor && !foundAfter) {
|
if (afterCursor && !foundAfter) {
|
||||||
if ([afterCursor isEqualToString:uri]) {
|
if ([afterCursor isEqualToString:uri]) {
|
||||||
foundAfter = YES;
|
foundAfter = YES;
|
||||||
@ -183,18 +216,18 @@ RCT_EXPORT_METHOD(getPhotos:(NSDictionary *)params
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
CGSize dimensions = [result defaultRepresentation].dimensions;
|
CGSize dimensions = [result defaultRepresentation].dimensions;
|
||||||
CLLocation *loc = [result valueForProperty:ALAssetPropertyLocation];
|
CLLocation *loc = [result valueForProperty:_ALAssetPropertyLocation];
|
||||||
NSDate *date = [result valueForProperty:ALAssetPropertyDate];
|
NSDate *date = [result valueForProperty:_ALAssetPropertyDate];
|
||||||
NSString *filename = [result defaultRepresentation].filename;
|
NSString *filename = [result defaultRepresentation].filename;
|
||||||
int64_t duration = 0;
|
int64_t duration = 0;
|
||||||
if ([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo]) {
|
if ([[result valueForProperty:_ALAssetPropertyType] isEqualToString:_ALAssetTypeVideo]) {
|
||||||
duration = [[result valueForProperty:ALAssetPropertyDuration] intValue];
|
duration = [[result valueForProperty:_ALAssetPropertyDuration] intValue];
|
||||||
}
|
}
|
||||||
|
|
||||||
[assets addObject:@{
|
[assets addObject:@{
|
||||||
@"node": @{
|
@"node": @{
|
||||||
@"type": [result valueForProperty:ALAssetPropertyType],
|
@"type": [result valueForProperty:_ALAssetPropertyType],
|
||||||
@"group_name": [group valueForProperty:ALAssetsGroupPropertyName],
|
@"group_name": [group valueForProperty:_ALAssetsGroupPropertyName],
|
||||||
@"image": @{
|
@"image": @{
|
||||||
@"uri": uri,
|
@"uri": uri,
|
||||||
@"filename" : filename ?: [NSNull null],
|
@"filename" : filename ?: [NSNull null],
|
||||||
|
Loading…
x
Reference in New Issue
Block a user