UpLoadImage.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import React from "react";
  2. import { Button, Layout, Icon } from "@ui-kitten/components";
  3. import { useModel } from "flooks";
  4. import { Linking } from "expo";
  5. import * as ImagePicker from "expo-image-picker";
  6. import { Image } from "react-native";
  7. import * as FileSystem from "expo-file-system";
  8. export default function UpLoadImage(props) {
  9. const { connect } = useModel("wordsModel");
  10. const { httpPost } = useModel("httpModel", true);
  11. const renderPulseIcon = (props) => <Icon {...props} name='plus-outline' />;
  12. return (
  13. <>
  14. <Button
  15. style={[props.style]}
  16. appearance='imageButton'
  17. onPress={() => {
  18. _pickImage().then((img) => {
  19. httpPost("/upload/base64", {
  20. base64: img,
  21. }).then((res) => {
  22. props.changeIcon(res);
  23. });
  24. });
  25. }}
  26. accessoryLeft={(imgprops) => {
  27. if (props.value && props.value != " ") {
  28. return (
  29. <Image
  30. style={{
  31. width: "100%",
  32. height: "100%",
  33. }}
  34. source={{ uri: props.value }}
  35. />
  36. );
  37. } else {
  38. return renderPulseIcon(imgprops);
  39. }
  40. }}
  41. />
  42. </>
  43. );
  44. }
  45. const getPermissionAsync = async () => {
  46. if (Constants.platform.ios) {
  47. const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
  48. if (status !== "granted") {
  49. alert("Sorry, we need camera roll permissions to make this work!");
  50. }
  51. }
  52. };
  53. const _pickImage = () => {
  54. return new Promise(async (resolve, reject) => {
  55. try {
  56. let permissionResult = await ImagePicker.requestCameraRollPermissionsAsync();
  57. if (permissionResult.granted === false) {
  58. reject("notAllod");
  59. } else {
  60. let result = await ImagePicker.launchImageLibraryAsync({
  61. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  62. allowsEditing: true,
  63. aspect: [1, 1],
  64. quality: 1,
  65. });
  66. if (!result.cancelled) {
  67. // let file = await FileSystem.readAsStringAsync(result.uri);
  68. resolve(result.uri);
  69. }
  70. }
  71. } catch (e) {
  72. reject(e);
  73. }
  74. });
  75. };