const i = new Image(); doesn't work on native iOS and Android


Dimitri Kopriwa

I am getting the following error in react-native:

[Unhandled promise rejection: ReferenceError: Can't find variable: Image]
* src/utils/b64ToBlob.js:45:56 in Promise$argument_0

Here is the function where it fails b64ToBlog.js:

export const getImageDimensions = (file) => new Promise((resolved) => {
  const i = new Image();
  i.onload = () => resolved(i);
  i.src = file;
});

On iOSand on , Androidit fails with :const i = new Image();

How to provide or get image dimensions Imagefrom URL , or how to provide it Imagein react-native ?

edit

For those interested, here is my final solution:

import { Platform, Image as ImageNative } from 'react-native';

export const getImageDimensions = (file) => new Promise((resolved) => {
  if (Platform.OS === 'web') {
    const i = new Image();
    i.onload = () => resolved(i);
    i.src = file;
    return i;
  } else {
    return getSizeNative(file).then(resolved);
  }
});

function getSizeNative(uri) {
  return new Promise((resolve, reject) => {
    ImageNative.getSize(uri, (width, height) => resolve({ width, height }), reject)
  })
}

TJ crowd

The code you show is for the DOM Imageconstructor , not the React Native Imagecomponent .

Looking at its API, it looks like React Native's Imagecomponents provide a getSizestatic method :

import { Image } from "react-native";

export const getImageDimensions = (file) => new Promise((resolved, reject) => {
    Image.getSize(
        file,
        (width, height) => resolve({width, height}),
        reject
    );
});

Sadly, the documentation doesn't seem to say how to report the size to the success function , so you might have to dig a little deeper or try it out and see what you get. ...but from the code subsequently posted in the question, it appears to be receiving them as discrete parameters, so I updated the above.

Related


const i = new Image(); doesn't work on native iOS and Android

Dimitri Kopriwa I am getting the following error in react-native: [Unhandled promise rejection: ReferenceError: Can't find variable: Image] * src/utils/b64ToBlob.js:45:56 in Promise$argument_0 Here is the function where it fails b64ToBlog.js: export const get

const i = new Image(); doesn't work on native iOS and Android

Dimitri Kopriwa I am getting the following error in react-native: [Unhandled promise rejection: ReferenceError: Can't find variable: Image] * src/utils/b64ToBlob.js:45:56 in Promise$argument_0 Here is the function where it fails b64ToBlog.js: export const get

const i = new Image(); doesn't work on native iOS and Android

Dimitri Kopriwa I am getting the following error in react-native: [Unhandled promise rejection: ReferenceError: Can't find variable: Image] * src/utils/b64ToBlob.js:45:56 in Promise$argument_0 Here is the function where it fails b64ToBlog.js: export const get

const i = new Image(); doesn't work on native iOS and Android

Dimitri Kopriwa I am getting the following error in react-native: [Unhandled promise rejection: ReferenceError: Can't find variable: Image] * src/utils/b64ToBlob.js:45:56 in Promise$argument_0 Here is the function where it fails b64ToBlog.js: export const get

Uploading image using fetch doesn't work in React Native (iOS)

Ikuhisa I'm developing a mobile app with React Native, but posting images on iOS doesn't work. I have connected my code to requestbin, set up info.plist to allow non-https urls and other post requests are working (eg login). For images, all I get is a blank bo

Flexbox doesn't work with <Image> in React Native Android

Erz I just started learning React-Native. I'm having trouble trying to render the image on the screen, but it doesn't appear. I have tried many solutions from google and stack overflow but can't solve the problem. Can someone please help me. Below are the deta

react-native-image-picker doesn't work in android 10

Hamza Elm I'm working with a project where I need to use the camera or select an image from the Library, so used React Native Image Picker which works fine in development mode and in production it doesn't work in Android 10, only me Tried a lot of solutions fr

Flexbox doesn't work with <Image> in React Native Android

Erz I just started learning React-Native. I'm having trouble trying to render the image on the screen, but it doesn't appear. I have tried many solutions from google and stack overflow but can't solve the problem. Can someone please help me. Below are the deta

react-native-image-picker doesn't work in android 10

Hamza Elm I'm working with a project where I need to use the camera or select an image from the Library, so used React Native Image Picker which works fine in development mode and in production it doesn't work in Android 10, only me Tried a lot of solutions fr

react-native-image-picker doesn't work in android 10

Hamza Elm I'm working with a project where I need to use the camera or select an image from the Library, so used React Native Image Picker which works fine in development mode and in production it doesn't work in Android 10, only me Tried a lot of solutions fr

Flexbox doesn't work with <Image> in React Native Android

Erz I just started learning React-Native. I'm having trouble trying to render the image on the screen, but it doesn't appear. I have tried many solutions from google and stack overflow but can't solve the problem. Can someone please help me. Below are the deta

Flexbox doesn't work with <Image> in React Native Android

Erz I just started learning React-Native. I'm having trouble trying to render the image on the screen, but it doesn't appear. I have tried many solutions from google and stack overflow but can't solve the problem. Can someone please help me. Below are the deta

react-native-image-picker doesn't work in android 10

Hamza Elm I'm working with a project where I need to use the camera or select an image from the Library, so used React Native Image Picker which works fine in development mode and in production it doesn't work in Android 10, only me Tried a lot of solutions fr

react-native-image-picker doesn't work in android 10

Hamza Elm I'm working with a project where I need to use the camera or select an image from the Library, so used React Native Image Picker which works fine in development mode and in production it doesn't work in Android 10, only me Tried a lot of solutions fr

SparkJava compile to native image doesn't work

xmcax : I'm trying to compile a simple spark-java based java application to a local image. Applications exposing a separate GET /healthcheckendpoint always return text/plain: OK. There are only two dependencies in the project: dependencies { implementation '

SparkJava compile to native image doesn't work

xmcax : I'm trying to compile a simple spark-java based java application to a local image. Applications exposing a separate GET /healthcheckendpoint always return text/plain: OK. There are only two dependencies in the project: dependencies { implementation '

SparkJava compile to native image doesn't work

xmcax : I'm trying to compile a simple spark-java based java application to a local image. Applications exposing a separate GET /healthcheckendpoint always return text/plain: OK. There are only two dependencies in the project: dependencies { implementation '

react native: importing image doesn't work

war commander I can't import images into my component. I saved the image in a file called WidgetImagesand Images.jswrote the following code in it to import it: var Images = { Weather: require('./WidgetsImages/Weather.png'), News: require('./WidgetsImag

GraalVM native image reflection doesn't work

Lacio I'm trying to create a GraalVM native image using the maven plugin and I'm running into some issues. Here is my configuration for maven plugin using GraalVM JDK (installed via Sdkman) : $ java -version openjdk version "16.0.1" 2021-04-20 OpenJDK Runtime

SparkJava compile to native image doesn't work

xmcax : I'm trying to compile a simple spark-java based java application to a local image. Applications exposing a separate GET /healthcheckendpoint always return text/plain: OK. There are only two dependencies in the project: dependencies { implementation '

SparkJava compile to native image doesn't work

xmcax : I'm trying to compile a simple spark-java based java application to a local image. Applications exposing a separate GET /healthcheckendpoint always return text/plain: OK. There are only two dependencies in the project: dependencies { implementation '

react-native-image-viewer - Landscape view doesn't work on IOS

Suther On Android, landscape mode works like a charm, but on IO I found the following issues: Mobile device is in landscape mode: I click on an image (Touchable-Opacity set state modalShow Landscape-Mode will switch to Portrait-Mode after which Modal appears I

react-native-image-viewer - Landscape view doesn't work on IOS

Suther On Android, landscape mode works like a charm, but on IO I found the following issues: Mobile device is in landscape mode: I click on an image (Touchable-Opacity set state modalShow Landscape-Mode will switch to Portrait-Mode after which Modal appears I