react-native-image-resizer icon indicating copy to clipboard operation
react-native-image-resizer copied to clipboard

keepMeta is not working on iOS

Open ishikawa-takumi opened this issue 3 years ago • 4 comments

I want to keep metadata when resizing an image. I used ImageResizer.createResizedImage with keepMeta = true. However, it is not working on iOS.

As a result of the analysis, getImageMeta() function is not working. It returns before getting the metadata from an image URI.

NSMutableDictionary * getImageMeta(NSString * path)
{
    if([path hasPrefix:@"assets-library"]) {

        __block NSMutableDictionary* res = nil;

        ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
        {
           // (1) here asynchronous processing
            NSDictionary *exif = [[myasset defaultRepresentation] metadata];
            res = [exif mutableCopy];

        };

        ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
        NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        [assetslibrary assetForURL:url resultBlock:resultblock failureBlock:^(NSError *error) { NSLog(@"error couldn't image from assets library"); }];

       // (2) result is returned before (1) is finished.
        return res;

    }

this issue can be prevented by using semaphore system.

environment

iPhoneSE 2generation (IOS 14.81) / iPhone12ProMax (iOS 15.1) react-native: 0.63.4 react-native-image-resizer: 1.4.5

ishikawa-takumi avatar Dec 10 '21 13:12 ishikawa-takumi

Hello, we've got exactly the same problem, on IOS the getImageMeta method returns null.

Is a fix on the way or is it exist a way to prevent that comportment ?

@ishikawa-takumi have you success to implement the semaphore patch you talked in your post ?

environment

iPhoneXR (IOS 15.1) / iPhone12 (iOS 15.1) react-native: 0.63.4 react-native-image-resizer: 1.4.5

odexvy avatar Feb 22 '22 08:02 odexvy

@odexvy Yes, I have prevented this issue applying the following code. node_modules/react-native-image-resizer/ios/RCTImageResizer.m

NSMutableDictionary * getImageMeta(NSString * path)
{
    if([path hasPrefix:@"assets-library"] ) {

        __block NSMutableDictionary* res = nil;
        dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);  
        @try {
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
                ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
                {

                    NSDictionary *exif = [[myasset defaultRepresentation] metadata];
                    res = [exif mutableCopy];
                    dispatch_semaphore_signal(semaphore);

                };

                ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
                NSURL *url = [NSURL URLWithString:[path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

                [assetslibrary assetForURL:url resultBlock:resultblock failureBlock:^(NSError *error) { NSLog(@"error couldn't image from assets library"); }];
            });
        } 
        @finally {
            dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC);
            dispatch_semaphore_wait(semaphore, time);
            return res;
        }
    } else {

ishikawa-takumi avatar Feb 23 '22 08:02 ishikawa-takumi

thank you very much @ishikawa-takumi 👍

odexvy avatar Feb 23 '22 17:02 odexvy

So keepMeta doesn't work for files in the assets library? Or it doesn't work for any file? I've been using it fine for images taken by the camera, saved into the app's local storage (documents folder), and then resizing it.

cristianoccazinsp avatar Jun 20 '22 18:06 cristianoccazinsp

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Sep 01 '22 18:09 stale[bot]

This issue has been automatically closed. Thank you for your contributions.

stale[bot] avatar Sep 08 '22 18:09 stale[bot]