1

I have a drawImage function in my application which loads the image from a blob and then from that image object calls the toDataURL function for the canvas to draw image. The problem is that this code works on android and browser but not working on iphone or ipad.

I have tried passing the image type to the toDataURL function and tried setting the default width and height of the canvas to prevent width getting 0 for the canvas but nothing seems to work on iphone or ipad. It only shows a blank screen inplace of the image.

the code works fine on mac/safari as well but not in iphone/ipad on any browser.

here is my drawImage function

_drawImage(img) {
    // console.log("drawimage");
    this.setState({ isGenerating: true });
    if (img) {
        fetch(img)
            .then((r) => r.blob())
            .then((blob) => {
                loadImage(
                    blob,
                    (img) => {
                        if (img.type === "error") {
                            this.setState({ error: true });
                            // console.log(img, "Error loading image ");
                        } else {
                            const image = new window.Image();
                            const height = this.props.height;
                            const width = this.props.width;
                            image.src = img.toDataURL('image/jpeg');
                            console.log(image.src, height, width)
                            image.onload = () => {
                                let offsetX,
                                    offsetY,
                                    renderableWidth = image.width,
                                    renderableHeight = image.height;
                                var imageDimensionRatio = image.width / image.height;
                                var canvasDimensionRatio = width / height;
                                // console.log(
                                //   "ratios of image and canvas =",
                                //   imageDimensionRatio,
                                //   canvasDimensionRatio
                                // );
                                if (imageDimensionRatio < canvasDimensionRatio) {
                                    renderableHeight = height;
                                    renderableWidth =
                                        image.width * (renderableHeight / image.height);
                                    offsetX = (width - renderableWidth) / 2;
                                    offsetY = 0;
                                } else if (imageDimensionRatio > canvasDimensionRatio) {
                                    renderableWidth = width;
                                    renderableHeight =
                                        image.height * (renderableWidth / image.width);
                                    offsetX = 0;
                                    offsetY = (height - renderableHeight) / 2;
                                } else {
                                    renderableHeight = height;
                                    renderableWidth = width;
                                    offsetX = 0;
                                    offsetY = 0;
                                }
                                console.log(renderableHeight, renderableWidth, image)
                                this.setState(
                                    {
                                        image: image,
                                        renderableHeight,
                                        renderableWidth,
                                        imgHeight: image.height,
                                        imgWidth: image.width,
                                        offsetX,
                                        offsetY,
                                        height,
                                        width,
                                    },
                                    () => {
                                        this.setState({ isGenerating: false });
                                    }
                                );
                            };
                        }
                    },
                    {
                        orientation: true,
                        canvas: true,
                    }
                );
            });
    }
}

EDIT I've solved the issue by passing maxWidth to loadImage params along with orientation and canvas, which limits the width to not to exceed in ios devices and thus onload function works properly. basically I've added the below code.

maxWidth: 10000
  • Does this answer your question? [toDataURL is not a function in ios](https://stackoverflow.com/questions/74532495/todataurl-is-not-a-function-in-ios) – Kaiido Nov 22 '22 at 13:23
  • @Kaiido not really the problem still remains and the question is basically why the `image.onload` is not getting triggered in ipad/iphone only? is there any size limit or dimmension wise because it works for smaller images. but for an image of around 12-15 MB the `onerror` is getting triggered instead of `onload`. – Ridham Sharma Nov 22 '22 at 13:36
  • The issue might be due to `blueimp-load-image` library which is providing me with the `loadImage` function. – Ridham Sharma Nov 22 '22 at 13:54
  • But both questions are the same aren't they? – Kaiido Nov 23 '22 at 01:25
  • yes, basically are two perspective of the same problem. – Ridham Sharma Nov 23 '22 at 11:30

0 Answers0