Created
August 16, 2016 17:41
-
-
Save ronaldheft/d244797e5ce2a8d954cd64fd494d120e to your computer and use it in GitHub Desktop.
React Native iOS 8+ Photo Library Support (with Android)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { CameraRoll } from 'react-native'; | |
const DEFAULT_MEDIA_OPTIONS = { | |
first: 1000 | |
}; | |
class MediaLibrary { | |
fetchMedia(opts = {}) { | |
const getPhotos = (media, after) => { | |
const options = {...DEFAULT_MEDIA_OPTIONS, ...opts, after}; | |
return CameraRoll.getPhotos(options).then(data => { | |
const assets = data.edges; | |
media = media.concat(assets.map(asset => asset.node.image)); | |
return data.page_info.has_next_page ? getPhotos(media, data.page_info.end_cursor) : media; | |
}); | |
}; | |
return getPhotos([]); | |
} | |
} | |
export default new MediaLibrary(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef MediaLibrary_h | |
#define MediaLibrary_h | |
#import <Foundation/Foundation.h> | |
#import "RCTViewManager.h" | |
@interface MediaLibrary : NSObject<RCTBridgeModule> | |
@end | |
#endif /* MediaLibrary_h */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { NativeModules } from 'react-native'; | |
const MediaLibraryManager = NativeModules.MediaLibrary; | |
const DEFAULT_MEDIA_OPTIONS = {}; | |
class MediaLibrary { | |
fetchMedia(opts = {}) { | |
const options = {...DEFAULT_MEDIA_OPTIONS, ...opts}; | |
return MediaLibraryManager.getMedia(options); | |
} | |
} | |
export default new MediaLibrary(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import "MediaLibrary.h" | |
#import <Photos/Photos.h> | |
@interface MediaLibrary () | |
@property (nonatomic, strong) PHCachingImageManager *imageManager; | |
@end | |
@implementation MediaLibrary | |
RCT_EXPORT_MODULE(); | |
RCT_EXPORT_METHOD(getMedia:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejector:(RCTPromiseRejectBlock)reject) | |
{ | |
[self checkPhotosPermissions:^(BOOL granted) { | |
if (!granted) { | |
reject(@"Permissions", @"Photos permission not granted", nil); | |
return; | |
} | |
[self fetchMedia:options resolver:resolve rejector:reject]; | |
}]; | |
} | |
- (PHCachingImageManager *)imageManager | |
{ | |
if (_imageManager == nil) { | |
_imageManager = [PHCachingImageManager new]; | |
} | |
return _imageManager; | |
} | |
- (void)checkPhotosPermissions:(void(^)(BOOL granted))callback | |
{ | |
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus]; | |
if (status == PHAuthorizationStatusAuthorized) { | |
callback(YES); | |
return; | |
} else if (status == PHAuthorizationStatusNotDetermined) { | |
[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) { | |
if (status == PHAuthorizationStatusAuthorized) { | |
callback(YES); | |
return; | |
} | |
else { | |
callback(NO); | |
return; | |
} | |
}]; | |
} | |
else { | |
callback(NO); | |
} | |
} | |
- (void)fetchMedia:(NSDictionary *)options resolver:(RCTPromiseResolveBlock)resolve rejector:(RCTPromiseRejectBlock)reject | |
{ | |
PHFetchOptions *fetchOptions = [PHFetchOptions new]; | |
fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]]; | |
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions]; | |
NSMutableArray *images = [[NSMutableArray alloc] init]; | |
for (PHAsset *asset in fetchResult) { | |
[images addObject:@{ | |
@"uri": [NSString stringWithFormat:@"ph://%@", asset.localIdentifier] | |
}]; | |
} | |
resolve(images); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment