如何在react native(expo)中显示blob URL

nhaq1z21  于 2023-06-24  发布在  React
关注(0)|答案(1)|浏览(166)

我试图得到一个图像显示使用blob url而不是本地文件。
这是一个示例blob url:blob:602342de-a839-442f-9c91-877301955502?offset=0&size=35
这就是代码:

import React, { useState } from "react";
import { Text, Image, TouchableOpacity, SafeAreaView } from "react-native";
import * as ImagePicker from "expo-image-picker";

function dataURItoBlob(dataURI) {
  // convert base64/URLEncoded data component to raw binary data held in a string
  var byteString;
  if (dataURI.split(",")[0].indexOf("base64") >= 0)
    byteString = atob(dataURI.split(",")[1]);
  else byteString = unescape(dataURI.split(",")[1]);

  // separate out the mime component
  var mimeString = dataURI.split(",")[0].split(":")[1].split(";")[0];

  // write the bytes of the string to a typed array
  var ia = new Uint8Array(byteString.length);
  for (var i = 0; i < byteString.length; i++) {
    ia[i] = byteString.charCodeAt(i);
  }

  return new Blob([ia], { type: mimeString });
}

export default function App() {
  const [blobUrl, setBlobUrl] = useState(null);

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

    if (!result.canceled) {
      let imgBlob = dataURItoBlob(result.assets[0].uri);
      let blobUrl = URL.createObjectURL(imgBlob);
      setBlobUrl(blobUrl);
      //  setBlobUrl(result.assets[0].uri);
      console.log(blobUrl);
    }
  };

  return (
    <SafeAreaView>
      <TouchableOpacity onPress={pickImage}>
        <Text>Pick Image</Text>
      </TouchableOpacity>

      {blobUrl && (
        <Image
          source={{ uri: blobUrl }}
          style={{ width: 300, height: 200, marginBottom: 5 }}
        />
      )}
    </SafeAreaView>
  );
}

如果我使用本地文件uri result.assets[0].uri,图像就会显示出来。但是使用blob uri,图像就不会这样。我有什么错吗?
如果你需要更多的细节,请告诉我。

rqqzpn5f

rqqzpn5f1#

问题在于使用dataURItoBlob函数将数据URI转换为Blob对象。您提供的函数似乎是为处理包含base64编码数据或URL编码数据的数据URI而设计的。但是,您提供的blob URL(例如,blob:602342 de-a839 - 442 f-9 c91 -877301955502?offset=0&size=35)不是数据URI,而是浏览器生成的URL。您可以直接使用blob URL作为图像源。

import React, { useState } from "react";
import { Text, Image, TouchableOpacity, SafeAreaView } from "react-native";
import * as ImagePicker from "expo-image-picker";

export default function App() {
  const [blobUrl, setBlobUrl] = useState(null);

  const pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: true,
      aspect: [4, 3],
      quality: 1,
      base64: false, // Don't request base64 data
    });

    if (!result.cancelled) {
      setBlobUrl(result.uri);
    }
  };

  return (
    <SafeAreaView>
      <TouchableOpacity onPress={pickImage}>
        <Text>Pick Image</Text>
      </TouchableOpacity>

      {blobUrl && (
        <Image
          source={{ uri: blobUrl }}
          style={{ width: 300, height: 200, marginBottom: 5 }}
        />
      )}
    </SafeAreaView>
  );
}

通过这些更改,当您使用图像选取器选择图像时,blob URL将被设置为图像源,并且它应该在组件中正确显示。

相关问题