Expo ImageManipulator
提供用于在本地文件系统上进行图片操作的 API 的库。
expo-image-manipulator 提供了一个 API 来修改存储在本地文件系统上的图片。
¥expo-image-manipulator provides an API to modify images stored on the local file system.
安装
¥Installation
- npx expo install expo-image-manipulatorIf you are installing this in an existing React Native app, make sure to install expo in your project.
用法
¥Usage
这将首先将图片顺时针旋转 90 度,然后垂直翻转旋转的图片并将其另存为 PNG。
¥This will first rotate the image 90 degrees clockwise, then flip the rotated image vertically and save it as a PNG.
import { useState } from 'react'; import { Button, Image, StyleSheet, View } from 'react-native'; import { Asset } from 'expo-asset'; import { FlipType, SaveFormat, useImageManipulator } from 'expo-image-manipulator'; const IMAGE = Asset.fromModule(require('./assets/snack-icon.png')); export default function App() { const [image, setImage] = useState(IMAGE); const context = useImageManipulator(IMAGE.uri); const rotate90andFlip = async () => { context.rotate(90).flip(FlipType.Vertical); const image = await context.renderAsync(); const result = await image.saveAsync({ format: SaveFormat.PNG, }); setImage(result); }; return ( <View style={styles.container}> <View style={styles.imageContainer}> <Image source={{ uri: image.localUri || image.uri }} style={styles.image} /> </View> <Button title="Rotate and Flip" onPress={rotate90andFlip} /> </View> ); } const styles = StyleSheet.create({ container: { flex: 1, justifyContent: 'center', }, imageContainer: { marginVertical: 20, alignItems: 'center', justifyContent: 'center', }, image: { width: 300, height: 300, resizeMode: 'contain', }, });
API
import * as ImageManipulator from 'expo-image-manipulator';
Constants
Type: ImageManipulator
Hooks
Classes
Type: Class extends _default
ImageManipulator Methods
| Parameter | Type |
|---|---|
| source | string | SharedRef<'image'> |
Loads an image from the given URI and creates a new image manipulation context.
ImageManipulatorContextType: Class extends SharedObject
A context for an image manipulation. It provides synchronous, chainable functions that schedule transformations on the original image to the background thread.
Use an asynchronous renderAsync to await for all transformations to finish and access the final image.
ImageManipulatorContext Methods
| Parameter | Type | Description |
|---|---|---|
| rect | {
height: number,
originX: number,
originY: number,
width: number
} | Fields specify top-left corner and dimensions of a crop rectangle. |
Crops the image to the given rectangle's origin and size.
ImageManipulatorContext| Parameter | Type |
|---|---|
| options | {
backgroundColor: null | string,
height: number,
originX: number,
originY: number,
width: number
} |
Set the image size and offset. If the image is enlarged, unfilled areas are set to the backgroundColor.
To position the image, use originX and originY.
ImageManipulatorContext| Parameter | Type | Description |
|---|---|---|
| flipType | 'vertical' | 'horizontal' | An axis on which image will be flipped. Only one flip per transformation is available. If you want to flip according to both axes then provide two separate transformations. |
Flips the image vertically or horizontally.
ImageManipulatorContextResets the manipulator context to the originally loaded image.
ImageManipulatorContext| Parameter | Type | Description |
|---|---|---|
| size | {
height: null | number,
width: null | number
} | Values correspond to the result image dimensions. If you specify only one value, the other will be calculated automatically to preserve image ratio. |
Resizes the image to the given size.
ImageManipulatorContext| Parameter | Type | Description |
|---|---|---|
| degrees | number | Degrees to rotate the image. Rotation is clockwise when the value is positive and counter-clockwise when negative. |
Rotates the image by the given number of degrees.
ImageManipulatorContextType: Class extends SharedRef<'image'>
A reference to a native instance of the image.
ImageRef Properties
ImageRef Methods
| Parameter | Type | Description |
|---|---|---|
| options(optional) | SaveOptions | A map defining how modified image should be saved. |
Saves the image to the file system in the cache directory.
Promise<ImageResult>Methods
Deprecated It has been replaced by the new, contextual and object-oriented API. Use
ImageManipulator.manipulateoruseImageManipulatorinstead.
| Parameter | Type | Description |
|---|---|---|
| uri | string | URI of the file to manipulate. Should be on the local file system or a base64 data URI. |
| actions(optional) | Action[] | An array of objects representing manipulation options. Each object should have only one of the keys that corresponds to specific transformation. Default: [] |
| saveOptions(optional) | SaveOptions | A map defining how modified image should be saved. Default: {} |
Manipulate the image provided via uri. Available modifications are rotating, flipping (mirroring),
resizing and cropping. Each invocation results in a new file. With one invocation you can provide
a set of actions to perform over the image. Overwriting the source file would not have an effect
in displaying the result as images are cached.
Promise<ImageResult>Promise which fulfils with ImageResult object.
Types
Literal Type: union
Acceptable values are: ActionResize | ActionRotate | ActionFlip | ActionCrop | ActionExtent
| Property | Type | Description |
|---|---|---|
| crop | {
height: number,
originX: number,
originY: number,
width: number
} | Fields specify top-left corner and dimensions of a crop rectangle. |
| Property | Type | Description |
|---|---|---|
| extent | {
backgroundColor: string | null,
height: number,
originX: number,
originY: number,
width: number
} | Only for: Web Set the image size and offset. If the image is enlarged, unfilled areas are set to the |
| Property | Type | Description |
|---|---|---|
| flip | FlipType | An axis on which image will be flipped. Only one flip per transformation is available. If you want to flip according to both axes then provide two separate transformations. |
| Property | Type | Description |
|---|---|---|
| resize | {
height: number,
width: number
} | Values correspond to the result image dimensions. If you specify only one value, the other will be calculated automatically to preserve image ratio. |
| Property | Type | Description |
|---|---|---|
| rotate | number | Degrees to rotate the image. Rotation is clockwise when the value is positive and counter-clockwise when negative. |
| Property | Type | Description |
|---|---|---|
| base64(optional) | string | It is included if the |
| height | number | Height of the image or video. |
| uri | string | An URI to the modified image (usable as the source for an |
| width | number | Width of the image or video. |
A map defining how modified image should be saved.
| Property | Type | Description |
|---|---|---|
| base64(optional) | boolean | Whether to also include the image data in Base64 format. |
| compress(optional) | number | A value in range |
| format(optional) | SaveFormat | Specifies what type of compression should be used and what is the result file extension.
|