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

[iOS] ImageResizer / Message: Image uri invalid

Open ggunti opened this issue 3 years ago • 1 comments

I use the following uri (obtained from camera): /var/mobile/Containers/Data/Application/6DCA648B-3E9E-4535-AC2C-20AA90F17681/Library/Caches/RNRectangleScanner/O1663320548.jpeg

imageWidth and imageHeight are set properly. Rotation is set to multiples of 90 (0, 90, 180 or 270).

But when I call

    createResizedImage(this.props.imageUri, imageWidth, imageHeight, 'JPEG', 100, this.state.rotation, undefined, false, {
      mode: 'stretch',
      onlyScaleDown: true,
    })

I get the following error: IMG_20220916_123501

What could cause this? Why the uri is considered invalid? Same happens when I try to use a remote uri. On android it works perfectly.

"@bam.tech/react-native-image-resizer": "^3.0.0",
"react": "17.0.2",
"react-native": "0.68.2"

EDIT: I tried to add "file://" at the beginning of the uri and it did not crash anymore. But the resulted image is not as expected when rotation is different from 0. On android works perfectly also with rotation.

ggunti avatar Sep 16 '22 09:09 ggunti

@ggunti

Thank you for your feedback. I'll take some time tomorrow or next week in order to give you a proper answer.

Until then, do not hesitate to give me the result of your investigation. It will help us for sure :)

taboulot avatar Sep 22 '22 16:09 taboulot

Use file:// before your uri

use this type:

file:///var/mobile/Containers/Data/Application/6DCA648B-3E9E-4535-AC2C-20AA90F17681/Library/Caches/RNRectangleScanner/O1663320548.jpeg

logicbuzzchirag avatar Sep 27 '22 05:09 logicbuzzchirag

FWIW, it might help a bit if https://github.com/bamlab/react-native-image-resizer/blob/master/ios/ImageResizer.mm#L46 raised something with the localizedDescription of the error instead of the generic Image uri invalid, particularly considering the documentation specifically says the argument is supposed to be a path or an encoded image string, not a URI.

kueda avatar Sep 28 '22 01:09 kueda

@logicbuzzchirag @kueda See the "EDIT" section of my previous comment. I solved the error, but the rotation does not work properly on iOS.

ggunti avatar Sep 28 '22 07:09 ggunti

Just tested both versions, and this is a regression compared to version 1.4.5. Where before plain path like /users/etc worked, now it need to be an URI.

HugoGresse avatar Oct 04 '22 15:10 HugoGresse

yes, we can add 'file://' before, but the problem is that rotation isn't working properly on iOS

ggunti avatar Oct 04 '22 16:10 ggunti

rotation is probably another issue. I think this issue can be closed either:

  • changing the release not to add the breaking change
  • patching it in this library.

HugoGresse avatar Oct 05 '22 07:10 HugoGresse

Hi folks 👋

Indeed I introduced a regression during the migration.

I do not feel comfortable with how you access files on iOS. The difference between path, URL, etc... is not crystal clear in my mind.

I think this should help to support both path and URI : https://gist.github.com/taboulot/2a56c43dd27479c31beb2066a1f803cd

I have tested it in the example project, which uses URI, and it works well. @ggunti Can you apply this patch on your project that use path and tell me if it fixes the problem?

taboulot avatar Oct 06 '22 11:10 taboulot

I'm also getting the same error for photos:// uris on iOS, which worked on a previous version of this library

YousefED avatar Nov 03 '22 08:11 YousefED

Hi @taboulot! It works for me! @ggunti can you confirm please?

nicolasdevienne avatar Nov 11 '22 16:11 nicolasdevienne

Hi @taboulot! It would be great to correct this issue to use the new lib :) Thx!

nicolasdevienne avatar Nov 24 '22 14:11 nicolasdevienne

In version 1.4.5 it works fine with path [ph://]. However, with version 3.0.4 it cannot work. Is there any good solution?

astecpaint avatar Dec 13 '22 00:12 astecpaint

Updated to 3.0.4 and faced the same issue. Trying to resize photos taken with react-native-vision-camera results in an 'Image uri invalid' error. Image path starts with /private/var/mobile.... Adding file:// before it fixes the issue. Is this a problem with resizer or camera?

nofacez avatar Dec 14 '22 14:12 nofacez

I used remote link (ex: https://www.nsbpictures.com/wp-content/uploads/2018/06/photo-frames-png-6-881x1024.png) or base64 with the right format but still got this issue

hungtd97 avatar Dec 19 '22 02:12 hungtd97

Is file:// necessary?

zhiqingchen avatar Jan 28 '23 08:01 zhiqingchen

Having this issue come up in my project. Will a fix for this be rolled into the package?

The workaround mentioned above regarding the appending of file:// seems to work, but an explanation as to why this works would be appreciated.

forkball avatar Feb 01 '23 18:02 forkball

I'm also getting this exact same error when passing a correctly encoded base64 string for the URI param - We've upgraded to the bam package from the previously maintained one and since the upgrade this error has occurred.

I can also confirm that the base64 string is valid as when applying the same string to the web, the image shows perfectly fine.

JoshWalshaw avatar Feb 15 '23 08:02 JoshWalshaw

For anyone that wants to apply the solution provided by @taboulot in form of a patch-package compatible file, create the file patches/@bam.tech+react-native-image-resizer+3.0.5.patch with the following content:

diff --git a/node_modules/@bam.tech/react-native-image-resizer/ios/ImageResizer.mm b/node_modules/@bam.tech/react-native-image-resizer/ios/ImageResizer.mm
index 694f8ba..45d049c 100644
--- a/node_modules/@bam.tech/react-native-image-resizer/ios/ImageResizer.mm
+++ b/node_modules/@bam.tech/react-native-image-resizer/ios/ImageResizer.mm
@@ -39,7 +39,8 @@ RCT_REMAP_METHOD(createResizedImage, uri:(NSString *)uri width:(double)width hei
                 [NSException raise:moduleName format:@"Invalid output path."];
             }
 
-            NSURL * fileURL = [[NSURL alloc] initWithString:uri];
+            NSURL *baseURL = [NSURL fileURLWithPath:@"file://"];
+            NSURL * fileURL = [[NSURL alloc] initWithString:uri relativeToURL:baseURL];
 
             NSError *err;
             if ([fileURL checkResourceIsReachableAndReturnError:&err] == NO){

FibreFoX avatar Apr 13 '23 13:04 FibreFoX

Any updates on this? I'm trying to give a Photo library path directly on iOS but it's crashing. I also need rotation to stay as-is so "file://" prefix workaround wouldn't work either.

canpoyrazoglu avatar Apr 26 '23 07:04 canpoyrazoglu

Any updates on this? I'm trying to give a Photo library path directly on iOS but it's crashing. I also need rotation to stay as-is so "file://" prefix workaround wouldn't work either. I also tried the `ImageResizer.mm patch with no success.

I am having the same issue. Actually, no matter whether I used uri or base64 this release would give the invalid URI error. I tried prepending on base64 Once I downgraded to 1.4.5 everything worked as expected.

initial uri: ph://05D3ED1F-C94A-4025-8950-E1C31BC4EA20/L0/001 1.4.5 success 3.0.5 fail altered uri: file://05D3ED1F-C94A-4025-8950-E1C31BC4EA20/L0/001.jpeg 3.05 fail base64 format: data:image/jpeg;base64,/9j/... 3.05 fail

ericmatlock avatar May 11 '23 15:05 ericmatlock

The solution here posted by @taboulot only works for the default native ios or android picker libraries. If you use the cameraroll library then it adds ph:// in front of the uri (asset.image.uri) and fails.

https://github.com/react-native-cameraroll/react-native-cameraroll

pribeh avatar May 31 '23 20:05 pribeh

How can such a critical issue not be resolved for almost a year?

vicrac avatar Jul 12 '23 10:07 vicrac

You would be expected to resolve such a critical error. It's been almost 1 year. Files starting with ph currently don't work in 3.0.5 Unbelievable. Good luck with your effort, but you'd better take a look now?

CMLCNL avatar Aug 12 '23 11:08 CMLCNL

I also cannot get this to work with uri values prefixed with ph://. I am using @react-native-camera-roll/camera-roll to select images and that package provides the image uri prefixed with ph://. An example of the full format is ph://ABC-123/A0/001.

I tried to edit the uri to instead prefix with file://, but that does not work, i still get the error.

@taboulot am I understanding correctly that the bamlab version of react-native-image-resizer (version 3.0.5) is not compatible with @react-native-camera-roll/camera-roll, and the only way I can use @react-native-camera-roll/camera-roll is to use the old non-bamlab version of react-native-image-resizer (version 1.4.5) which is depricated? I can confirm that version 1.4.5 still does work with @react-native-camera-roll/camera-roll with the ph:// format.

jp-23 avatar Aug 17 '23 13:08 jp-23

Hi there,

I had time to fix the initial problem in the following PR: #381.

The problem was initially introduced by me during a refactor. Major problems with absolute path (using react-native-vision-camera) or different uri providers (like react-native-image-picker & @react-native-camera-roll/camera-roll) have been fixed.

There is still an issue with uri starting with ph://, but it looks like it is related to ReactNative. You can take a closer look at the PR. Help/PR on this subject is welcome.

I'm actually waiting for internal reviews and then I will release it (in 1-2 days).

You can still pull the branch to test it on your project & give me some feedback directly inside the PR.


📣 One last word. @vicrac & @CMLCNL I understand your frustration about the time it took for me to solve this & I am very sorry about that. However, as you may have noticed, this repository is open source & totally free. That means BAM and/or me are taking/investing time in it during professional/personal time depending on priorities we have. Open source is only working with the help, vision, contribution, etc... of people. And tools like Github are perfectly designed for that. Next time, feel free to investigate, fork, and create a minimal example repository. It will help a lot.

taboulot avatar Aug 21 '23 16:08 taboulot

Thank you so much. You are a hero. @taboulot

CMLCNL avatar Aug 21 '23 16:08 CMLCNL

Can't you just prepend "file://" to your path when calling ImageResizer.createResizeImage() in your client code (instead of depending on the library to do it)? That works for me in iOS, but haven't tested Android yet.

rcase100 avatar Aug 21 '23 17:08 rcase100

@rcase100 I thought about doing it. But If I do that it will introduce again the issue with camera-roll library that return an uri starting with ph://. I prefer to use the solution we had before v3.x.x until I fully understand how uri works on both iOS & Android :)

taboulot avatar Aug 21 '23 18:08 taboulot

Fix released in 3.0.6

taboulot avatar Aug 22 '23 08:08 taboulot

@taboulot rotation still not working on ios. I'm using 3.0.7

pycxxx avatar Aug 31 '23 03:08 pycxxx