UpLoadImage.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. console.log(imgprops);
  28. console.log(props);
  29. if (props.value && props.value != " ") {
  30. return (
  31. <Image
  32. style={{
  33. width: "100%",
  34. height: "100%",
  35. }}
  36. source={props.value}
  37. />
  38. );
  39. } else {
  40. return renderPulseIcon(imgprops);
  41. }
  42. }}
  43. />
  44. </>
  45. );
  46. }
  47. const getPermissionAsync = async () => {
  48. if (Constants.platform.ios) {
  49. const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
  50. if (status !== "granted") {
  51. alert("Sorry, we need camera roll permissions to make this work!");
  52. }
  53. }
  54. };
  55. const _pickImage = () => {
  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. });
  68. if (!result.cancelled) {
  69. // let file = await FileSystem.readAsStringAsync(result.uri);
  70. resolve(result.uri);
  71. }
  72. }
  73. } catch (e) {
  74. reject(e);
  75. }
  76. });
  77. };