use OOP api

This commit is contained in:
Samuel Newman
2025-04-26 20:29:55 +03:00
parent ea718190ca
commit 90e51df36b
2 changed files with 15 additions and 15 deletions
+11 -11
View File
@@ -1,5 +1,5 @@
import {Image as RNImage, Share as RNShare} from 'react-native' import {Image as RNImage, Share as RNShare} from 'react-native'
import {Image} from 'react-native-image-crop-picker' import {type Image} from 'react-native-image-crop-picker'
import uuid from 'react-native-uuid' import uuid from 'react-native-uuid'
import { import {
cacheDirectory, cacheDirectory,
@@ -11,7 +11,7 @@ import {
StorageAccessFramework, StorageAccessFramework,
writeAsStringAsync, writeAsStringAsync,
} from 'expo-file-system' } from 'expo-file-system'
import {manipulateAsync, SaveFormat} from 'expo-image-manipulator' import {ImageManipulator, SaveFormat} from 'expo-image-manipulator'
import * as MediaLibrary from 'expo-media-library' import * as MediaLibrary from 'expo-media-library'
import * as Sharing from 'expo-sharing' import * as Sharing from 'expo-sharing'
import {Buffer} from 'buffer' import {Buffer} from 'buffer'
@@ -20,7 +20,7 @@ import RNFetchBlob from 'rn-fetch-blob'
import {POST_IMG_MAX} from '#/lib/constants' import {POST_IMG_MAX} from '#/lib/constants'
import {logger} from '#/logger' import {logger} from '#/logger'
import {isAndroid, isIOS} from '#/platform/detection' import {isAndroid, isIOS} from '#/platform/detection'
import {Dimensions} from './types' import {type Dimensions} from './types'
export async function compressIfNeeded( export async function compressIfNeeded(
img: Image, img: Image,
@@ -172,23 +172,23 @@ async function doResize(localUri: string, opts: DoResizeOpts): Promise<Image> {
// Now instead, we have to supply the final dimensions to the manipulation function instead. // Now instead, we have to supply the final dimensions to the manipulation function instead.
// Performing an "empty" manipulation lets us get the dimensions of the original image. React Native's Image.getSize() // Performing an "empty" manipulation lets us get the dimensions of the original image. React Native's Image.getSize()
// does not work for local files... // does not work for local files...
const imageRes = await manipulateAsync(localUri, [], {}) const context = ImageManipulator.manipulate(localUri)
const imageRes = await context.renderAsync()
const newDimensions = getResizedDimensions({ const newDimensions = getResizedDimensions({
width: imageRes.width, width: imageRes.width,
height: imageRes.height, height: imageRes.height,
}) })
imageRes.release()
context.resize(newDimensions)
const resizedImage = await context.renderAsync()
for (let i = 0; i < 9; i++) { for (let i = 0; i < 9; i++) {
// nearest 10th // nearest 10th
const quality = Math.round((1 - 0.1 * i) * 10) / 10 const quality = Math.round((1 - 0.1 * i) * 10) / 10
const resizeRes = await manipulateAsync( const resizeRes = await resizedImage.saveAsync({
localUri,
[{resize: newDimensions}],
{
format: SaveFormat.JPEG, format: SaveFormat.JPEG,
compress: quality, compress: quality,
}, })
)
const fileInfo = await getInfoAsync(resizeRes.uri) const fileInfo = await getInfoAsync(resizeRes.uri)
if (!fileInfo.exists) { if (!fileInfo.exists) {
@@ -198,7 +198,7 @@ async function doResize(localUri: string, opts: DoResizeOpts): Promise<Image> {
} }
if (fileInfo.size < opts.maxSize) { if (fileInfo.size < opts.maxSize) {
safeDeleteAsync(imageRes.uri) resizedImage.release()
return { return {
path: normalizePath(resizeRes.uri), path: normalizePath(resizeRes.uri),
mime: 'image/jpeg', mime: 'image/jpeg',
+2 -2
View File
@@ -1,6 +1,6 @@
import {Image as RNImage} from 'react-native-image-crop-picker' import {type Image as RNImage} from 'react-native-image-crop-picker'
import {Dimensions} from './types' import {type Dimensions} from './types'
import {blobToDataUri, getDataUriSize} from './util' import {blobToDataUri, getDataUriSize} from './util'
export async function compressIfNeeded( export async function compressIfNeeded(