Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make react-native-image-picker compatible with use_frameworks #2242

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 30 additions & 31 deletions ios/ImagePickerManager.mm
Original file line number Diff line number Diff line change
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,15 +95,15 @@ - (void)launchImagePicker:(NSDictionary *)options callback:(RCTResponseSenderBlo
} else {
[self showPickerViewController:picker];
}

return;
}
}
#endif
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[ImagePickerUtils setupPickerFromOptions:picker options:self.options target:target];
picker.delegate = self;

if([self.options[@"includeExtra"] boolValue]) {
[self checkPhotosPermissions:^(BOOL granted) {
if (!granted) {
Expand All @@ -131,7 +130,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 @@ -141,11 +140,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 @@ -162,7 +161,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 @@ -178,7 +177,7 @@ -(NSMutableDictionary *)mapImageToAsset:(UIImage *)image data:(NSData *)data phA
data = UIImagePNGRepresentation(newImage);
}
}

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

Expand All @@ -203,13 +202,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 @@ -236,10 +235,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 @@ -259,25 +258,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 @@ -288,14 +287,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 @@ -306,7 +305,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 @@ -456,12 +455,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 @@ -512,7 +511,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 @@ -535,7 +534,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 @@ -549,7 +548,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
Original file line number Diff line number Diff line change
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function has been defined in 0.70 and allows library maintainers not to care about the internals of React Native. We are going to handle all the dependencies on the internal pods ourselves and, as long as you use this function, you don't have to care about them.

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