wxik / react-native-rich-editor

Lightweight React Native (JavaScript, H5) rich text editor
https://wxik.github.io/react-native-rich-editor/web
MIT License
799 stars 303 forks source link

Cannot display local image resources #230

Open qzlu-cyber opened 2 years ago

qzlu-cyber commented 2 years ago

It doesn't show up when I've selected an image. it prompts me 'Not allowed to load local resource: file:///Users/xxxxxx/Library/Developer/CoreSimulator/Devices/xxxxxx/data/Containers/Data/Application/xxxxx/tmp/xxx.jpg'. This is my code:

  const selectImage = async () => {
    try {
      const result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.Images,
        allowsEditing: true,
      });
      if (!result.cancelled) setImageUri(result.base64);
    } catch (error) {
      console.log("Error reading an image", error);
    }
  };

  let onPressAddImage = useCallback(() => {
    selectImage();
    // insert URL
    richText.current?.insertImage(imageUri);
    // insert base64
    // this.richText.current?.insertImage(`data:${image.mime};base64,${image.data}`);
  }, []);

When I click to add an image, onPressAddImage is called, but the image is not displayed. I have the same problem with this #217 But according to his method I didn't solve the problem. Could you please help me? @rafaelboniolo

rafaelboniolo commented 2 years ago

I created a correction PR but it still hasn't been accepted, it seems to me the error still persists in production!

rafaelboniolo commented 2 years ago

Sorry, in production my PR works correctly

qzlu-cyber commented 2 years ago

I solved this problem temporarily after using base64. Thank you very much

DevVarsha commented 2 years ago

I solved it like

import ImgToBase64 from 'react-native-image-base64';
import ImagePicker from "react-native-image-crop-picker";

 openGalleryClickProfile() {
    ImagePicker.openPicker({
      width: 300,
      height: 400,
      cropping: true,
    }).then((image) => {
      console.log("Imagemime", image); 
     this.onPressAddImage(image)
    });
  }

  onPressAddImage(image){
    ImgToBase64.getBase64String(image.path)
    .then((base64String) => {
      const str = `data:${image.mime};base64,${base64String}`
      this.richText.current?.insertImage(
        str
      );
    })
    .catch((err) => {
      console.log("base64:Image:", err)
    })};
certified84 commented 2 months ago

I created a correction PR but it still hasn't been accepted, it seems to me the error still persists in production!

Can you link me to that PR? I'd like to see your implementation for fixing it because I'm also facing the same issue