Media library access via new expo-media-library Query API, image resize for wallpaper export, and MiDaS depth map service stub for V1.5.
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
import * as MediaLibrary from "expo-media-library";
|
|
import * as ImageManipulator from "expo-image-manipulator";
|
|
import { Dimensions } from "react-native";
|
|
|
|
const { width: SCREEN_WIDTH, height: SCREEN_HEIGHT } = Dimensions.get("screen");
|
|
|
|
export async function requestGalleryPermission(): Promise<boolean> {
|
|
const { status } = await MediaLibrary.requestPermissionsAsync();
|
|
return status === "granted";
|
|
}
|
|
|
|
export async function getRecentPhotos(
|
|
count: number = 50,
|
|
offset: number = 0
|
|
): Promise<{ assets: MediaLibrary.Asset[]; hasMore: boolean }> {
|
|
const query = new MediaLibrary.Query()
|
|
.eq(MediaLibrary.AssetField.MEDIA_TYPE, MediaLibrary.MediaType.IMAGE)
|
|
.orderBy({
|
|
key: MediaLibrary.AssetField.CREATION_TIME,
|
|
ascending: false,
|
|
})
|
|
.limit(count)
|
|
.offset(offset);
|
|
|
|
const assets = await query.exe();
|
|
|
|
return {
|
|
assets,
|
|
hasMore: assets.length === count,
|
|
};
|
|
}
|
|
|
|
export async function prepareImageForWallpaper(
|
|
uri: string
|
|
): Promise<string> {
|
|
const result = await ImageManipulator.manipulateAsync(
|
|
uri,
|
|
[
|
|
{
|
|
resize: {
|
|
width: SCREEN_WIDTH * 2,
|
|
height: SCREEN_HEIGHT * 2,
|
|
},
|
|
},
|
|
],
|
|
{ compress: 0.95, format: ImageManipulator.SaveFormat.JPEG }
|
|
);
|
|
|
|
return result.uri;
|
|
}
|