UpLoadImage.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, Platform } 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. console.log(img);
  20. httpPost("/upload/base64", {
  21. base64: img,
  22. }).then((res) => {
  23. props.changeIcon(res);
  24. });
  25. });
  26. }}
  27. accessoryLeft={(imgprops) => {
  28. if (props.value && props.value != " ") {
  29. return (
  30. <Image
  31. style={{
  32. width: "100%",
  33. height: "100%",
  34. }}
  35. source={{ uri: props.value }}
  36. />
  37. );
  38. } else {
  39. return renderPulseIcon(imgprops);
  40. }
  41. }}
  42. />
  43. </>
  44. );
  45. }
  46. const getPermissionAsync = async () => {
  47. if (Constants.platform.ios) {
  48. const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
  49. if (status !== "granted") {
  50. alert("Sorry, we need camera roll permissions to make this work!");
  51. }
  52. }
  53. };
  54. const _pickImage = () => {
  55. console.log(Platform);
  56. return new Promise(async (resolve, reject) => {
  57. try {
  58. let permissionResult = await ImagePicker.requestCameraRollPermissionsAsync();
  59. if (permissionResult.granted === false) {
  60. reject("notAllod");
  61. } else {
  62. let result = await ImagePicker.launchImageLibraryAsync({
  63. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  64. allowsEditing: true,
  65. aspect: [1, 1],
  66. quality: 1,
  67. base64: true,
  68. });
  69. if (!result.cancelled) {
  70. if (Platform.OS == "web") {
  71. resolve(result.uri);
  72. } else {
  73. resolve(result.base64);
  74. }
  75. }
  76. }
  77. } catch (e) {
  78. reject(e);
  79. }
  80. });
  81. };