0
// write image to local path
  NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES).firstObject;
  NSString *filePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"a.jpg"]];
  [UIImagePNGRepresentation(image)writeToFile:filePath atomically:YES]

// under iOS (it works great)
[UIImage imageWithContentsOfFile: filePath];

// under react native (Could not find image filePath)
<Image source={{ uri:  filePath }} />

This is the result no image

Anyone has the same issues?


I gave this a shot, but no good!

<Image style={{width:100, height:100}} source={{ uri: 'file://' + RNFS.DocumentDirectoryPath + '/myAwesomeSubDir/my.png', scale:1}} /> 

I knew why Image doesn't render the source, cuz the source path has no extension name! Image doesn't render local source (path with no extension name)

DavidLee
  • 53
  • 1
  • 9
  • In iOS you cannot guarantee that the filePath to the image is constant between builds. Every time you run `react-native run-ios` the file path will change. This means that you have to construct the filePath each time you want to use the file. You cannot hardcore the filePath. – Andrew Mar 08 '19 at 07:05
  • @Andrew Yes, under ios `[UIImage imageWithContentsOfFile: imagePath]` everything goes well. Is there a way to solve it? – DavidLee Mar 08 '19 at 07:23
  • How are you saving the image to the device? Are you saving it in the native or JavaScript side? – Andrew Mar 08 '19 at 08:22
  • @Andrew I'm using a native instant messaging Framework! The framework save image to device. I write the chat UI by React native. – DavidLee Mar 08 '19 at 08:51
  • If you look at the native code it is constructing the image path, you need to do the same. This may mean you need to install a dependency like `react-native-fs` or `rn-fetch-blob` that will give you access to the device filesystem. Using one of those you should be able to create the correct file path to the image. see this [SO question](https://stackoverflow.com/questions/39286211/react-native-ios-read-image-from-apps-documents-folder/45378735) for an example – Andrew Mar 08 '19 at 08:55
  • @Andrew I'll give it a shot! Thanks. I'll let you know if it works! – DavidLee Mar 08 '19 at 09:01
  • @Andrew No good! – DavidLee Mar 08 '19 at 09:38
  • @DavidLee I am also having a problem in iOS (not on android), accessing files stored locally such as file:///data/user/0/com.xxxx/files/productdata_da13681addffaed7622ece9b2bb56e22bc65f6b9.jpg. I tried to access such files in the browser, couldn't as well. Any idea? – Yossi Jun 27 '20 at 16:07

0 Answers0