UpLoadImage.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* eslint-disable react/destructuring-assignment */
  2. /* eslint-disable no-async-promise-executor */
  3. /* eslint-disable prefer-promise-reject-errors */
  4. /* eslint-disable no-underscore-dangle */
  5. import React from "react";
  6. import { Button, Icon } from "@ui-kitten/components";
  7. import { useModel } from "flooks";
  8. import * as ImagePicker from "expo-image-picker";
  9. import { Image, Platform } from "react-native";
  10. const _pickImage = () => {
  11. return new Promise(async (resolve, reject) => {
  12. try {
  13. const permissionResult = await ImagePicker.requestCameraRollPermissionsAsync();
  14. if (permissionResult.granted === false) {
  15. reject("notAllod");
  16. } else {
  17. const result = await ImagePicker.launchImageLibraryAsync({
  18. mediaTypes: ImagePicker.MediaTypeOptions.Images,
  19. allowsEditing: true,
  20. aspect: [1, 1],
  21. quality: 1,
  22. base64: true,
  23. });
  24. if (!result.cancelled) {
  25. if (Platform.OS === "web") {
  26. resolve(result.uri);
  27. } else {
  28. resolve(result.base64);
  29. }
  30. }
  31. }
  32. } catch (e) {
  33. reject(e);
  34. }
  35. });
  36. };
  37. export default function UpLoadImage(props) {
  38. const { httpPost } = useModel("httpModel", true);
  39. // eslint-disable-next-line no-shadow
  40. const renderPulseIcon = props => <Icon {...props} name="plus-outline" />;
  41. return (
  42. <>
  43. <Button
  44. style={[
  45. props.style,
  46. props.size ? { width: props.size, height: props.size } : {},
  47. ]}
  48. appearance="imageButton"
  49. onPress={() => {
  50. _pickImage().then(img => {
  51. httpPost("/upload/base64", {
  52. base64: img,
  53. }).then(res => {
  54. props.changeIcon(res);
  55. });
  56. });
  57. }}
  58. accessoryLeft={imgprops => {
  59. if (props.value && props.value !== " ") {
  60. return (
  61. <Image
  62. style={{
  63. width: "100%",
  64. height: "100%",
  65. }}
  66. source={{ uri: props.value }}
  67. />
  68. );
  69. }
  70. return renderPulseIcon(imgprops);
  71. }}
  72. />
  73. {/* {props.hasCancel === true && props.value && (
  74. <Button
  75. size="tiny"
  76. status="danger"
  77. accessoryLeft={ForwardIcon}
  78. appearance="imgButton"
  79. style={{
  80. position: "absolute",
  81. right: -5,
  82. top: -5,
  83. borderRadius: 30,
  84. }}
  85. onPress={() => {
  86. showDialog({
  87. bodyText: removeTips,
  88. status: "danger",
  89. cancelable: true,
  90. confirmCallback: () => {
  91. props.delEvent();
  92. },
  93. });
  94. }}
  95. />
  96. )} */}
  97. </>
  98. );
  99. }