UpLoadImage.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, removeTips } = useModel("wordsModel");
  10. const { httpPost } = useModel("httpModel", true);
  11. const { showDialog } = useModel("dialogModel", true);
  12. const renderPulseIcon = (props) => <Icon {...props} name='plus-outline' />;
  13. return (
  14. <>
  15. <Button
  16. style={[
  17. props.style,
  18. props.size ? { width: props.size, height: props.size } : {},
  19. ]}
  20. appearance='imageButton'
  21. onPress={() => {
  22. _pickImage().then((img) => {
  23. console.log(img);
  24. httpPost("/upload/base64", {
  25. base64: img,
  26. }).then((res) => {
  27. props.changeIcon(res);
  28. });
  29. });
  30. }}
  31. accessoryLeft={(imgprops) => {
  32. if (props.value && props.value != " ") {
  33. return (
  34. <Image
  35. style={{
  36. width: "100%",
  37. height: "100%",
  38. }}
  39. source={{ uri: props.value }}
  40. />
  41. );
  42. } else {
  43. return renderPulseIcon(imgprops);
  44. }
  45. }}
  46. />
  47. {props.hasCancel && props.value && (
  48. <Button
  49. size='tiny'
  50. status='danger'
  51. accessoryLeft={ForwardIcon}
  52. appearance='imgButton'
  53. style={{
  54. position: "absolute",
  55. right: -5,
  56. top: -5,
  57. borderRadius: 30,
  58. }}
  59. onPress={() => {
  60. showDialog({
  61. bodyText: removeTips,
  62. status: "danger",
  63. cancelable: true,
  64. confirmCallback: () => {
  65. props.delEvent();
  66. },
  67. });
  68. }}
  69. />
  70. )}
  71. </>
  72. );
  73. }
  74. const ForwardIcon = (props) => <Icon {...props} name='minus' fill='#fff' />;
  75. const getPermissionAsync = async () => {
  76. if (Constants.platform.ios) {
  77. const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
  78. if (status !== "granted") {
  79. alert("Sorry, we need camera roll permissions to make this work!");
  80. }
  81. }
  82. };
  83. const _pickImage = () => {
  84. console.log(Platform);
  85. return new Promise(async (resolve, reject) => {
  86. try {
  87. let permissionResult = await ImagePicker.requestCameraRollPermissionsAsync();
  88. if (permissionResult.granted === false) {
  89. reject("notAllod");
  90. } else {
  91. let result = await ImagePicker.launchImageLibraryAsync({
  92. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  93. allowsEditing: true,
  94. aspect: [1, 1],
  95. quality: 1,
  96. base64: true,
  97. });
  98. if (!result.cancelled) {
  99. if (Platform.OS == "web") {
  100. resolve(result.uri);
  101. } else {
  102. resolve(result.base64);
  103. }
  104. }
  105. }
  106. } catch (e) {
  107. reject(e);
  108. }
  109. });
  110. };