Skip to content

Commit

Permalink
feat: add use_frameworks compatibility (#2242)
Browse files Browse the repository at this point in the history
  • Loading branch information
cipolleschi committed Dec 4, 2023
1 parent 5a79ea8 commit 6cb36f4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 48 deletions.
59 changes: 29 additions & 30 deletions ios/ImagePickerManager.mm
Expand Up @@ -4,8 +4,7 @@
#import <AVFoundation/AVFoundation.h>
#import <Photos/Photos.h>
#import <PhotosUI/PhotosUI.h>

@import MobileCoreServices;
#import <MobileCoreServices/MobileCoreServices.h>

@interface ImagePickerManager ()

Expand Down Expand Up @@ -67,12 +66,12 @@ @implementation ImagePickerManager
- (void)launchImagePicker:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback
{
self.callback = callback;

if (target == camera && [ImagePickerUtils isSimulator]) {
self.callback(@[@{@"errorCode": errCameraUnavailable}]);
return;
}

self.options = options;

#if __has_include(<PhotosUI/PHPicker.h>)
Expand All @@ -85,7 +84,7 @@ - (void)launchImagePicker:(NSDictionary *)options callback:(RCTResponseSenderBlo
picker.presentationController.delegate = self;

if([self.options[@"includeExtra"] boolValue]) {

[self checkPhotosPermissions:^(BOOL granted) {
if (!granted) {
self.callback(@[@{@"errorCode": errPermission}]);
Expand All @@ -96,7 +95,7 @@ - (void)launchImagePicker:(NSDictionary *)options callback:(RCTResponseSenderBlo
} else {
[self showPickerViewController:picker];
}

return;
}
}
Expand Down Expand Up @@ -132,7 +131,7 @@ - (void) showPickerViewController:(UIViewController *)picker
NSData* extractImageData(UIImage* image){
CFMutableDataRef imageData = CFDataCreateMutable(NULL, 0);
CGImageDestinationRef destination = CGImageDestinationCreateWithData(imageData, kUTTypeJPEG, 1, NULL);

CFStringRef orientationKey[1];
CFTypeRef orientationValue[1];
CGImagePropertyOrientation CGOrientation = CGImagePropertyOrientationForUIImageOrientation(image.imageOrientation);
Expand All @@ -142,11 +141,11 @@ - (void) showPickerViewController:(UIViewController *)picker

CFDictionaryRef imageProps = CFDictionaryCreate( NULL, (const void **)orientationKey, (const void **)orientationValue, 1,
&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

CGImageDestinationAddImage(destination, image.CGImage, imageProps);

CGImageDestinationFinalize(destination);

CFRelease(destination);
CFRelease(orientationValue[0]);
CFRelease(imageProps);
Expand All @@ -163,7 +162,7 @@ -(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phA
}
data = extractImageData(image);
}

UIImage* newImage = image;
if (![fileType isEqualToString:@"gif"]) {
newImage = [ImagePickerUtils resizeImage:image
Expand All @@ -179,7 +178,7 @@ -(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phA
data = UIImagePNGRepresentation(newImage);
}
}

NSMutableDictionary *asset = [[NSMutableDictionary alloc] init];
asset[@"type"] = [@"image/" stringByAppendingString:fileType];

Expand All @@ -204,13 +203,13 @@ -(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phA
asset[@"fileName"] = fileName;
asset[@"width"] = @(newImage.size.width);
asset[@"height"] = @(newImage.size.height);

if(phAsset){
asset[@"timestamp"] = [self getDateTimeInUTC:phAsset.creationDate];
asset[@"id"] = phAsset.localIdentifier;
// Add more extra data here ...
}

return asset;
}

Expand All @@ -237,10 +236,10 @@ -(NSMutableDictionary *)mapVideoToAsset:(NSURL *)url phAsset:(PHAsset * _Nullabl
if ((target == camera) && [self.options[@"saveToPhotos"] boolValue]) {
UISaveVideoAtPathToSavedPhotosAlbum(url.path, nil, nil, nil);
}

if (![url.URLByResolvingSymlinksInPath.path isEqualToString:videoDestinationURL.URLByResolvingSymlinksInPath.path]) {
NSFileManager *fileManager = [NSFileManager defaultManager];

// Delete file if it already exists
if ([fileManager fileExistsAtPath:videoDestinationURL.path]) {
[fileManager removeItemAtURL:videoDestinationURL error:nil];
Expand All @@ -260,25 +259,25 @@ -(NSMutableDictionary *)mapVideoToAsset:(NSURL *)url phAsset:(PHAsset * _Nullabl
}
}
}

NSMutableDictionary *response = [[NSMutableDictionary alloc] init];

if([self.options[@"formatAsMp4"] boolValue] && ![fileExtension isEqualToString:@"mp4"]) {
NSURL *parentURL = [videoDestinationURL URLByDeletingLastPathComponent];
NSString *path = [[parentURL.path stringByAppendingString:@"/"] stringByAppendingString:[[NSUUID UUID] UUIDString]];
path = [path stringByAppendingString:@".mp4"];
NSURL *outputURL = [NSURL fileURLWithPath:path];

[[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil];
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:videoDestinationURL options:nil];
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];

exportSession.outputURL = outputURL;
exportSession.outputFileType = AVFileTypeMPEG4;
exportSession.shouldOptimizeForNetworkUse = YES;

dispatch_semaphore_t sem = dispatch_semaphore_create(0);

[exportSession exportAsynchronouslyWithCompletionHandler:^(void) {
if (exportSession.status == AVAssetExportSessionStatusCompleted) {
CGSize dimentions = [ImagePickerUtils getVideoDimensionsFromUrl:outputURL];
Expand All @@ -289,14 +288,14 @@ -(NSMutableDictionary *)mapVideoToAsset:(NSURL *)url phAsset:(PHAsset * _Nullabl
response[@"fileSize"] = [ImagePickerUtils getFileSizeFromUrl:outputURL];
response[@"width"] = @(dimentions.width);
response[@"height"] = @(dimentions.height);

dispatch_semaphore_signal(sem);
} else if (exportSession.status == AVAssetExportSessionStatusFailed || exportSession.status == AVAssetExportSessionStatusCancelled) {
dispatch_semaphore_signal(sem);
}
}];


dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
} else {
CGSize dimentions = [ImagePickerUtils getVideoDimensionsFromUrl:videoDestinationURL];
Expand All @@ -307,7 +306,7 @@ -(NSMutableDictionary *)mapVideoToAsset:(NSURL *)url phAsset:(PHAsset * _Nullabl
response[@"fileSize"] = [ImagePickerUtils getFileSizeFromUrl:videoDestinationURL];
response[@"width"] = @(dimentions.width);
response[@"height"] = @(dimentions.height);

if(phAsset){
response[@"timestamp"] = [self getDateTimeInUTC:phAsset.creationDate];
response[@"id"] = phAsset.localIdentifier;
Expand Down Expand Up @@ -457,12 +456,12 @@ - (void)imagePickerController:(UIImagePickerController *)picker didFinishPicking

if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *) kUTTypeImage]) {
UIImage *image = [ImagePickerManager getUIImageFromInfo:info];

[assets addObject:[self mapImageToAsset:image data:[NSData dataWithContentsOfURL:[ImagePickerManager getNSURLFromInfo:info]] phAsset:asset]];
} else {
NSError *error;
NSDictionary *videoAsset = [self mapVideoToAsset:info[UIImagePickerControllerMediaURL] phAsset:asset error:&error];

if (videoAsset == nil) {
NSString *errorMessage = error.localizedFailureReason;
if (errorMessage == nil) errorMessage = @"Video asset not found";
Expand Down Expand Up @@ -513,7 +512,7 @@ - (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPick
return;
}
photoSelected = YES;

if (results.count == 0) {
dispatch_async(dispatch_get_main_queue(), ^{
self.callback(@[@{@"didCancel": @YES}]);
Expand All @@ -536,7 +535,7 @@ - (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPick
PHFetchResult* fetchResult = [PHAsset fetchAssetsWithLocalIdentifiers:@[result.assetIdentifier] options:nil];
asset = fetchResult.firstObject;
}

dispatch_group_enter(completionGroup);

if ([provider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
Expand All @@ -550,7 +549,7 @@ - (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPick
[provider loadFileRepresentationForTypeIdentifier:identifier completionHandler:^(NSURL * _Nullable url, NSError * _Nullable error) {
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];

assets[index] = [self mapImageToAsset:image data:data phAsset:asset];
dispatch_group_leave(completionGroup);
}];
Expand Down
42 changes: 24 additions & 18 deletions react-native-image-picker.podspec
Expand Up @@ -14,26 +14,32 @@ Pod::Spec.new do |s|

s.source = { :git => "https://github.com/react-native-image-picker/react-native-image-picker.git", :tag => "v#{s.version}" }
s.source_files = "ios/*.{h,m,mm}"
s.pod_target_xcconfig = { 'OTHER_CPLUSPLUSFLAGS' => '-fcxx-modules' }

if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
s.frameworks = "MobileCoreServices"

s.pod_target_xcconfig = {
'HEADER_SEARCH_PATHS' => '"$(PODS_ROOT)/boost" "$(PODS_ROOT)/boost-for-react-native" "$(PODS_ROOT)/RCT-Folly"',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17'
}

s.compiler_flags = folly_compiler_flags + ' -DRN_FABRIC_ENABLED -fmodules -fcxx-modules'

s.dependency "React"
s.dependency "React-RCTFabric" # This is for fabric component
s.dependency "React-Codegen"
s.dependency "RCT-Folly"
s.dependency "RCTRequired"
s.dependency "RCTTypeSafety"
s.dependency "ReactCommon/turbomodule/core"
if defined?(install_modules_dependencies) != nil
install_modules_dependencies(s)
else
s.dependency "React-Core"

if ENV['RCT_NEW_ARCH_ENABLED'] == '1'
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'

s.pod_target_xcconfig = {
'HEADER_SEARCH_PATHS' => '"$(PODS_ROOT)/boost" "$(PODS_ROOT)/boost-for-react-native" "$(PODS_ROOT)/RCT-Folly"',
'CLANG_CXX_LANGUAGE_STANDARD' => 'c++17'
}

s.compiler_flags = folly_compiler_flags + ' -DRN_FABRIC_ENABLED -fmodules -fcxx-modules'

s.dependency "React"
s.dependency "React-RCTFabric" # This is for fabric component
s.dependency "React-Codegen"
s.dependency "RCT-Folly"
s.dependency "RCTRequired"
s.dependency "RCTTypeSafety"
s.dependency "ReactCommon/turbomodule/core"
else
s.dependency "React-Core"
end
end
end

0 comments on commit 6cb36f4

Please sign in to comment.