Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web app crashes on Image update #2432

Closed
luizv opened this issue May 19, 2024 · 2 comments
Closed

Web app crashes on Image update #2432

luizv opened this issue May 19, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@luizv
Copy link

luizv commented May 19, 2024

Description

When trying to update image prop passed to ReactNativeSkia.Image component, it crashes with the error:

Cannot pass deleted object as a pointer of type sk_sp

CleanShot 2024-05-19 at 16 35 02@2x

Version

1.2.3

Steps to reproduce

  1. On React Native Skia building for web, implement a simple image picker.
import { useState } from "react";
import { Button, View, StyleSheet } from "react-native";
import * as ImagePicker from "expo-image-picker";
import { CanvasComponent } from "./CanvasComponent";

export default function PickerComponent() {
  const [image, setImage] = useState<string | null>(null);

  const pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: false,
      aspect: [2, 4],
      quality: 1,
    });

    if (!result.canceled) {
      setImage(result.assets[0].uri);
    }
  };

  return (
    <View style={styles.container}>
      <Button title="Pick an image from camera roll" onPress={pickImage} />
      {image && <CanvasComponent imageSource={image} />}
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flexDirection: "column",
    alignItems: "center",
    justifyContent: "center",
  },
});
  1. When selecting a new image, updates it on a Skia Image inside an Skia Canvas.
import { Canvas, Image, useImage } from "@shopify/react-native-skia";

interface CanvasComponentProps {
  imageSource: string;
}
export const CanvasComponent = ({ imageSource }: CanvasComponentProps) => {

  const image = useImage(imageSource);

  return (
    <Canvas
      style={{
        backgroundColor: "red",
        width: 500,
        height: 500,
        alignContent: "center",
      }}
    >
      <Image 
        image={image}
        fit="cover"
        width={500}
        height={500} />
    </Canvas>
  );
};
  1. Updating the Image a couple times will crash the app on web with a error:

Cannot pass deleted object as a pointer of type sk_sp

Snack, code example, screenshot, or link to a repository

https://snack.expo.dev/@xluizv/skia-update-image-bug

(At Snack I couldn't make the setup for running react-native-skia on the web, thought the code is there and working with iOS at least)

@luizv luizv added the bug Something isn't working label May 19, 2024
@luizv luizv changed the title Image update generate crash on web Web app crashes on Image update May 19, 2024
@doctordru
Copy link

I found a solution to this problem, which is to use the lower level API to create the image. Here's an example for that code

import axios from 'axios'
import { Skia } from '@shopify/react-native-skia'

const response = await axios.get(url, {
        responseType: 'arraybuffer',
      })
const base64 = Buffer.from(response.data, 'binary').toString('base64')
const data = Skia.Data.fromBase64(base64)
const image = Skia.Image.MakeImageFromEncoded(data)
return image

I wrapped this in a react-query query in my codebase so that it's cached and it's removed the deleted reference error. Hope this helps @luizv

@wcandillon
Copy link
Contributor

@doctordru could you provide more context if it's something we could solve on our side?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants