useImage
图片操作, 如预览、获取、压缩等.
何时使用
当需要对图片进行操作时
API
const [
fileInfo,
{ choose, compress, get, preview, previewMedia, save, edit }
] = useImage(option?);
参数说明
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
option | 初始拾取图片配置(若指定后面可与新的配置合并) | ExcludeOption<Taro.chooseImage.Option> | - |
返回值说明
返回值 | 说明 | 类型 |
---|---|---|
fileInfo | 当前选中的图片 | Omit<Taro.chooseImage.SuccessCallbackResult, 'errMsg'> |
choose | 选择(会话)图片 | PromiseParamsAction<(option?: ChooseOption \| ExcludeOption<Taro.chooseMessageFile.Option>, messageFile?: boolean) => void, Taro。chooseImage.SuccessCallbackResult \| Taro.chooseMessageFile.SuccessCallbackResult> |
compress | 压缩图片 | PromiseParamsAction<(src: string, quality?: number) => void, CompressSuccessResult> |
get | 获取图片资源详情 | PromiseAction<string, Taro.getImageInfo.SuccessCallbackResult> |
preview | 预览 | PromiseAction<ExcludeOption<Taro.previewImage.Option>> |
previewMedia | 预览图片和视频 | PromiseAction<ExcludeOption<Taro.previewMedia.Option>> |
save | 保存图片到系统相册 | PromiseAction<string> |
edit | 编辑图片接口 | PromiseAction<string, Taro.editImage.SuccessCallbackResult> |
代码演示
- React
- Vue
media/useImage/index
import React from 'react';
import { useState } from '@taro-hooks/core';
import { escapeState } from '@taro-hooks/shared';
import { useToast, useImage } from 'taro-hooks';
import { Image } from '@tarojs/components';
import DemoContent from '@src/components/DemoContent';
import { Button, Swiper, Cell } from '@taroify/core';
import './index.less';
export default () => {
const { show } = useToast({ title: 'useImage', mask: true });
const [imageInfo, setImageInfo] =
useState<Taro.getImageInfo.SuccessCallbackResult>();
const [
fileInfo,
{ choose, compress, get, preview, previewMedia, save, edit },
] = useImage({
count: 15,
});
const handleGetFileInfo = async () => {
try {
const info = await get(escapeState(fileInfo)?.tempFilePaths?.[0]);
setImageInfo(info);
} catch (e) {
show({
title: '获取信息失败',
icon: 'error',
});
}
};
const handleCompress = async () => {
try {
const info = await compress(escapeState(fileInfo)?.tempFilePaths?.[0]);
setImageInfo(info);
} catch (e) {
show({
title: '压缩失败',
icon: 'error',
});
}
};
return (
<DemoContent>
<Swiper className="demo-swiper gap" height={200}>
{(fileInfo?.tempFilePaths ?? []).map((url) => (
<Swiper.Item key={url}>
<Image className="demo-swiper-item" src={url} />
</Swiper.Item>
))}
</Swiper>
<Button
block
color="primary"
className="gap"
onClick={() => choose()}
shape="square"
>
选择图片
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => preview({ urls: fileInfo?.tempFilePaths })}
shape="square"
>
预览照片
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => save(fileInfo?.tempFilePaths?.[0])}
shape="square"
>
保存照片
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => handleGetFileInfo()}
shape="square"
>
获取图片信息
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => choose({}, true)}
shape="square"
>
获取会话文件(仅小程序)
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => edit(fileInfo?.tempFilePaths?.[0])}
shape="square"
>
编辑图片(仅小程序)
</Button>
<Button
block
color="primary"
className="gap"
onClick={() =>
previewMedia({ sources: [{ url: fileInfo?.tempFilePaths?.[0] }] })
}
shape="square"
>
预览媒体(仅小程序)
</Button>
<Button
block
color="primary"
className="gap"
onClick={() => handleCompress()}
shape="square"
>
压缩图片
</Button>
<Cell.Group title="图片信息">
{Object.entries(imageInfo ?? {}).map(([key, value]) => (
<Cell key={key} title={key} brief={JSON.stringify(value)}></Cell>
))}
</Cell.Group>
</DemoContent>
);
};
media/useImage/index
<template>
<demo-content>
<nut-swiper height="200" class="demo-swiper gap">
<nut-swiper-item
v-for="(url, index) in fileInfo?.tempFilePaths"
:key="index"
>
<image class="demo-swiper-item" :src="url"></image>
</nut-swiper-item>
</nut-swiper>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="choose()"
>选择图片</nut-button
>
<nut-button
shape="square"
type="primary"
class="gap"
block
@click="preview({ urls: fileInfo?.tempFilePaths })"
>预览照片</nut-button
>
<nut-button
class="gap"
shape="square"
type="primary"
block
@click="save(fileInfo?.tempFilePaths?.[0])"
>保存照片</nut-button
>
<nut-button
class="gap"
shape="square"
type="primary"
block
@click="handleGetFileInfo()"
>获取图片信息</nut-button
>
<nut-button
class="gap"
shape="square"
type="primary"
block
@click="choose({}, true)"
>获取会话文件(仅小程序)</nut-button
>
<nut-button
class="gap"
shape="square"
type="primary"
block
@click="edit(fileInfo?.tempFilePaths?.[0])"
>编辑图片(仅小程序)</nut-button
>
<nut-button
class="gap"
shape="square"
type="primary"
block
@click="
previewMedia({ sources: [{ url: fileInfo?.tempFilePaths?.[0] }] })
"
>预览媒体(仅小程序)</nut-button
>
<nut-button
class="gap"
shape="square"
type="primary"
block
@click="handleCompress()"
>压缩图片</nut-button
>
<nut-cell-group title="图片信息">
<nut-cell
v-for="(value, key) in imageInfo"
:key="key"
:title="key"
:sub-title="$filters.stringify(value)"
></nut-cell>
</nut-cell-group>
</demo-content>
</template>
<script setup lang="ts">
import { useState } from '@taro-hooks/core';
import { escapeState } from '@taro-hooks/shared';
import { useToast, useImage } from 'taro-hooks';
const { show } = useToast({ title: 'useImage', mask: true });
const [imageInfo, setImageInfo] =
useState<Taro.getImageInfo.SuccessCallbackResult>();
const [fileInfo, { choose, compress, get, preview, previewMedia, save, edit }] =
useImage({
count: 15,
});
const handleGetFileInfo = async () => {
try {
const info = await get(escapeState(fileInfo)?.tempFilePaths?.[0]);
setImageInfo(info);
} catch (e) {
show({
title: '获取信息失败',
icon: 'error',
});
}
};
const handleCompress = async () => {
try {
const info = await compress(escapeState(fileInfo)?.tempFilePaths?.[0]);
setImageInfo(info);
} catch (e) {
show({
title: '压缩失败',
icon: 'error',
});
}
};
</script>
<style>
.demo-swiper,
.demo-swiper-item {
height: 200px;
}
.nut-swiper-item {
text-align: center;
}
</style>
Hook 支持度
微信小程序 | H5 | ReactNative |
---|---|---|
✔️ | ✔️ | ✔️ |