Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ios/RNPhotosFramework/PHAssetsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ typedef NS_OPTIONS(NSUInteger, AssetEnumerationDirection) {
};

+(PHFetchResult<PHAsset *> *) getAssetsForParams:(NSDictionary *)params;
+(NSArray<NSDictionary *> *) assetsArrayToUriArray:(NSArray<id> *)assetsArray andincludeMetadata:(BOOL)includeMetadata andIncludeAssetResourcesMetadata:(BOOL)includeResourcesMetadata;
+(void) assetsArrayToUriArray:(NSArray<id> *)assetsArray andincludeMetadata:(BOOL)includeMetadata andIncludeAssetResourcesMetadata:(BOOL)includeResourcesMetadata withCompletionBlock:(void (^)(NSArray<NSDictionary *> *arr))completionBlock;
+(NSMutableArray<PHAssetWithCollectionIndex*> *) getAssetsForFetchResult:(PHFetchResult *)assetsFetchResult startIndex:(int)startIndex endIndex:(int)endIndex assetDisplayStartToEnd:(BOOL)assetDisplayStartToEnd andAssetDisplayBottomUp:(BOOL)assetDisplayBottomUp;
+(NSMutableArray<PHAssetWithCollectionIndex*> *) getAssetsForFetchResult:(PHFetchResult *)assetsFetchResult atIndecies:(NSArray<NSNumber *> *)indecies;
+(PHFetchResult<PHAsset *> *) getAssetsFromArrayOfLocalIdentifiers:(NSArray<NSString *> *)arrayWithLocalIdentifiers;
+(NSMutableDictionary *)extendAssetDictWithAssetMetadata:(NSMutableDictionary *)dictToExtend andPHAsset:(PHAsset *)asset;
+(NSMutableDictionary *)extendAssetDictWithAssetResourcesMetadata:(NSMutableDictionary *)dictToExtend andPHAsset:(PHAsset *)asset;
+(void)extendAssetDictWithAssetResourcesMetadata:(NSMutableDictionary *)dictToExtend andPHAsset:(PHAsset *)asset withCompletionBlock:(void (^)(NSMutableDictionary *dict))completionBlock;
+(void)extendAssetDictWithPhotoAssetEditingMetadata:(NSMutableDictionary *)dictToExtend andPHAsset:(PHAsset *)asset andCompletionBlock:(void(^)(NSMutableDictionary * dict))completeBlock;
+(void)deleteAssets:(PHFetchResult<PHAsset *> *)assetsToDelete andCompleteBLock:(nullable void(^)(BOOL success, NSError *__nullable error, NSArray<NSString *> * localIdentifiers))completeBlock;
+(void)updateAssetWithParams:(NSDictionary *)params completionBlock:(void(^)(BOOL success, NSError * _Nullable error, NSString * _Nullable localIdentifier))completionBlock andAsset:(PHAsset *)asset;
Expand Down
92 changes: 68 additions & 24 deletions ios/RNPhotosFramework/PHAssetsService.m
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,20 @@ @implementation PHAssetsService
return [PHAsset fetchAssetsWithOptions:options];
}

+(NSArray<NSDictionary *> *) assetsArrayToUriArray:(NSArray<id> *)assetsArray andincludeMetadata:(BOOL)includeMetadata andIncludeAssetResourcesMetadata:(BOOL)includeResourcesMetadata {
+(void) assetsArrayToUriArray:(NSArray<id> *)assetsArray andincludeMetadata:(BOOL)includeMetadata andIncludeAssetResourcesMetadata:(BOOL)includeResourcesMetadata withCompletionBlock:(void (^)(NSArray<NSDictionary *> *arr))completionBlock{
RCT_PROFILE_BEGIN_EVENT(0, @"-[RCTCameraRollRNPhotosFrameworkManager assetsArrayToUriArray", nil);

NSMutableArray *uriArray = [NSMutableArray arrayWithCapacity:assetsArray.count];
NSDictionary *reveredMediaTypes = [RCTConvert PHAssetMediaTypeValuesReversed];
__block NSMutableSet *remainingAssets = [NSMutableSet setWithArray:assetsArray];

for(int i = 0;i < assetsArray.count; i++) {
id assetObj = [assetsArray objectAtIndex:i];
NSNumber *assetIndex = (NSNumber *)[NSNull null];
PHAsset *asset;
if([assetObj isKindOfClass:[PHAsset class]]) {
asset = assetObj;
}else {
} else {
PHAssetWithCollectionIndex *assetWithCollectionIndex = assetObj;
asset = assetWithCollectionIndex.asset;
assetIndex = assetWithCollectionIndex.collectionIndex;
Expand All @@ -91,15 +93,24 @@ @implementation PHAssetsService
[self extendAssetDictWithAssetMetadata:responseDict andPHAsset:asset];
}
if(includeResourcesMetadata) {
[self extendAssetDictWithAssetResourcesMetadata:responseDict andPHAsset:asset];
[self extendAssetDictWithAssetResourcesMetadata:responseDict andPHAsset:asset withCompletionBlock:^(NSMutableDictionary *dict) {
[uriArray addObject:responseDict];
[remainingAssets removeObject:assetObj];
if ([remainingAssets count] == 0) {
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
completionBlock(uriArray);
}
}];
} else {

[uriArray addObject:responseDict];
[remainingAssets removeObject:assetObj];
if ([remainingAssets count] == 0) {
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
completionBlock(uriArray);
}
}


[uriArray addObject:responseDict];
}
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");

return uriArray;
}

+(NSMutableDictionary *)extendAssetDictWithAssetMetadata:(NSMutableDictionary *)dictToExtend andPHAsset:(PHAsset *)asset {
Expand Down Expand Up @@ -135,11 +146,11 @@ +(NSMutableDictionary *)extendAssetDictWithAssetMetadata:(NSMutableDictionary *)
return dictToExtend;
}

+(NSMutableDictionary *)extendAssetDictWithAssetResourcesMetadata:(NSMutableDictionary *)dictToExtend andPHAsset:(PHAsset *)asset {

+(void)extendAssetDictWithAssetResourcesMetadata:(NSMutableDictionary *)dictToExtend andPHAsset:(PHAsset *)asset withCompletionBlock:(void (^)(NSMutableDictionary *dict))completionBlock{
NSArray<PHAssetResource *> *resources = [PHAssetResource assetResourcesForAsset:asset];
NSMutableArray *arrayWithResourcesMetadata = [NSMutableArray new];

__block NSMutableSet *remainingResources = [NSMutableSet setWithArray:resources];

for(int i = 0; i < resources.count;i++) {
PHAssetResource *resourceMetadata = [resources objectAtIndex:i];

Expand All @@ -148,20 +159,53 @@ +(NSMutableDictionary *)extendAssetDictWithAssetResourcesMetadata:(NSMutableDict
if(mimeTypeCString != nil) {
mimeType = (__bridge NSString *)(mimeTypeCString);
}

[arrayWithResourcesMetadata addObject:@{
@"originalFilename" : resourceMetadata.originalFilename,
@"assetLocalIdentifier" : resourceMetadata.assetLocalIdentifier,
@"uniformTypeIdentifier" : resourceMetadata.uniformTypeIdentifier,
@"type" : [[RCTConvert PHAssetResourceTypeValuesReversed] objectForKey:@(resourceMetadata.type)],
@"mimeType" : mimeType,
@"fileExtension" : [resourceMetadata.originalFilename pathExtension]
}];
[self videoUrlForLivePhotoAsset:asset andVideoResource:resourceMetadata withCompletionBlock:^(NSURL *url) {
[arrayWithResourcesMetadata addObject:@{
@"originalFilename" : resourceMetadata.originalFilename,
@"assetLocalIdentifier" : resourceMetadata.assetLocalIdentifier,
@"uniformTypeIdentifier" : resourceMetadata.uniformTypeIdentifier,
@"type" : [[RCTConvert PHAssetResourceTypeValuesReversed] objectForKey:@(resourceMetadata.type)],
@"mimeType" : mimeType,
@"fileExtension" : [resourceMetadata.originalFilename pathExtension],
@"videoUrl": url ? [url absoluteString] : [NSNull null]
}];
[remainingResources removeObject:resourceMetadata];
if ([remainingResources count] == 0) {
[dictToExtend setObject:arrayWithResourcesMetadata forKey:@"resourcesMetadata"];
completionBlock(dictToExtend);
}
}];
}
}

[dictToExtend setObject:arrayWithResourcesMetadata forKey:@"resourcesMetadata"];

return dictToExtend;
+(void)videoUrlForLivePhotoAsset:(PHAsset*)asset andVideoResource:(PHAssetResource *)videoResource withCompletionBlock:(void (^)(NSURL* url))completionBlock{
if(![asset isKindOfClass:[PHAsset class]] || videoResource.type != PHAssetResourceTypePairedVideo){
return completionBlock(nil);
}
NSString* filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.mov",[NSString stringWithFormat:@"%@", [[asset localIdentifier] stringByReplacingOccurrencesOfString:@"/" withString:@""]]]];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];

PHLivePhotoRequestOptions* options = [PHLivePhotoRequestOptions new];
options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
options.networkAccessAllowed = YES;
[[PHImageManager defaultManager] requestLivePhotoForAsset:asset targetSize:[UIScreen mainScreen].bounds.size contentMode:PHImageContentModeDefault options:options resultHandler:^(PHLivePhoto * _Nullable livePhoto, NSDictionary * _Nullable info) {
if(livePhoto){
NSError *err;
if ([fileUrl checkResourceIsReachableAndReturnError:&err] == NO) {
[[PHAssetResourceManager defaultManager] writeDataForAssetResource:videoResource toFile:fileUrl options:nil completionHandler:^(NSError * _Nullable error) {
if(!error){
completionBlock(fileUrl);
} else {
completionBlock(nil);
}
}];
} else {
completionBlock(fileUrl);
}
} else {
completionBlock(nil);
}
}];
}

+(void)extendAssetDictWithPhotoAssetEditingMetadata:(NSMutableDictionary *)dictToExtend andPHAsset:(PHAsset *)asset andCompletionBlock:(void(^)(NSMutableDictionary * dict))completeBlock {
Expand Down
47 changes: 27 additions & 20 deletions ios/RNPhotosFramework/PHChangeObserver.m
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,14 @@ -(void)removeChangeObserver {

- (void)photoLibraryDidChange:(PHChange *)changeInstance {
if(changeInstance != nil) {

dispatch_group_t group = dispatch_group_create();
NSMutableDictionary<NSString *, PHCachedFetchResult *> *previousFetches = [[PHCache sharedPHCache] fetchResults];

[previousFetches enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull uuid, PHCachedFetchResult * _Nonnull cachedFetchResult, BOOL * _Nonnull stop) {

PHFetchResultChangeDetails *changeDetails = [changeInstance changeDetailsForFetchResult:cachedFetchResult.fetchResult];

if(changeDetails != nil) {


BOOL trackInsertsAndDeletes = [RCTConvert BOOL:cachedFetchResult.originalFetchParams[@"trackInsertsAndDeletes"]];
BOOL trackChanges = [RCTConvert BOOL:cachedFetchResult.originalFetchParams[@"trackChanges"]];

Expand Down Expand Up @@ -77,15 +75,19 @@ - (void)photoLibraryDidChange:(PHChange *)changeInstance {
if([object isKindOfClass:[PHAsset class]]) {
BOOL includeMetadata = [RCTConvert BOOL:cachedFetchResult.originalFetchParams[@"includeMetadata"]];
BOOL includeResourcesMetadata = [RCTConvert BOOL:cachedFetchResult.originalFetchParams[@"includeResourcesMetadata"]];

NSDictionary *insertedObject = [[PHAssetsService assetsArrayToUriArray:@[object] andincludeMetadata:includeMetadata andIncludeAssetResourcesMetadata:includeResourcesMetadata] objectAtIndex:0];
NSNumber *collectionIndex = [insertedIndexes objectAtIndex:i];
NSMutableDictionary *mutableInsertedDict = [insertedObject mutableCopy];
[mutableInsertedDict setObject:collectionIndex forKey:@"collectionIndex"];
[insertedObjects addObject:@{
@"index" : collectionIndex,
@"obj" : mutableInsertedDict
}];
dispatch_group_enter(group);
[PHAssetsService assetsArrayToUriArray:@[object] andincludeMetadata:includeMetadata andIncludeAssetResourcesMetadata:includeResourcesMetadata withCompletionBlock:^(NSArray<NSDictionary *> *arr) {
NSDictionary *insertedObject = [arr objectAtIndex:0];
NSNumber *collectionIndex = [insertedIndexes objectAtIndex:i];
NSMutableDictionary *mutableInsertedDict = [insertedObject mutableCopy];
[mutableInsertedDict setObject:collectionIndex forKey:@"collectionIndex"];
[insertedObjects addObject:@{
@"index" : collectionIndex,
@"obj" : mutableInsertedDict
}];
dispatch_group_leave(group);
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
}
}
}
Expand All @@ -112,14 +114,19 @@ - (void)photoLibraryDidChange:(PHChange *)changeInstance {

BOOL includeMetadata = [RCTConvert BOOL:cachedFetchResult.originalFetchParams[@"includeMetadata"]];
BOOL includeResourcesMetadata = [RCTConvert BOOL:cachedFetchResult.originalFetchParams[@"includeResourcesMetadata"]];
NSDictionary *changedObject = [[PHAssetsService assetsArrayToUriArray:@[object] andincludeMetadata:includeMetadata andIncludeAssetResourcesMetadata:includeResourcesMetadata] objectAtIndex:0];
NSNumber *collectionIndex = [changedIndexes objectAtIndex:i];
NSMutableDictionary *mutableChangedDict = [changedObject mutableCopy];
[mutableChangedDict setObject:collectionIndex forKey:@"collectionIndex"];
[changedObjects addObject:@{
@"index" : collectionIndex,
@"obj" : mutableChangedDict
}];
dispatch_group_enter(group);
[PHAssetsService assetsArrayToUriArray:@[object] andincludeMetadata:includeMetadata andIncludeAssetResourcesMetadata:includeResourcesMetadata withCompletionBlock:^(NSArray<NSDictionary *> *arr) {
NSDictionary *changedObject = [arr objectAtIndex:0];
NSNumber *collectionIndex = [insertedIndexes objectAtIndex:i];
NSMutableDictionary *mutableChangedDict = [changedObject mutableCopy];
[mutableChangedDict setObject:collectionIndex forKey:@"collectionIndex"];
[changedObjects addObject:@{
@"index" : collectionIndex,
@"obj" : mutableChangedDict
}];
dispatch_group_leave(group);
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
}

}
Expand Down
9 changes: 7 additions & 2 deletions ios/RNPhotosFramework/PHCollectionService.m
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,13 @@ + (NSMutableDictionary *)generateAlbumResponseFromCollection:(PHCollection *)col
if(numberOfPreviewAssets > 0) {
BOOL assetDisplayStartToEnd = [RCTConvert BOOL:assetFetchParams[@"assetDisplayStartToEnd"]];
BOOL assetDisplayBottomUp = [RCTConvert BOOL:assetFetchParams[@"assetDisplayBottomUp"]];
NSArray<NSDictionary *> *previewAssets = [PHAssetsService assetsArrayToUriArray:[PHAssetsService getAssetsForFetchResult:assets startIndex:0 endIndex:(numberOfPreviewAssets-1) assetDisplayStartToEnd:assetDisplayStartToEnd andAssetDisplayBottomUp:assetDisplayBottomUp] andincludeMetadata:NO andIncludeAssetResourcesMetadata:resourcesMetadata];
[albumDictionary setObject:previewAssets forKey:@"previewAssets"];
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
[PHAssetsService assetsArrayToUriArray:[PHAssetsService getAssetsForFetchResult:assets startIndex:0 endIndex:(numberOfPreviewAssets - 1) assetDisplayStartToEnd:assetDisplayStartToEnd andAssetDisplayBottomUp:assetDisplayBottomUp] andincludeMetadata:NO andIncludeAssetResourcesMetadata:resourcesMetadata withCompletionBlock:^(NSArray<NSDictionary *> *arr) {
[albumDictionary setObject:arr forKey:@"previewAssets"];
dispatch_group_leave(group);
}];
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
}

}
Expand Down
Loading