跳过内容

开始使用

GitHub

安装

安装

您可以使用我们的AI辅助设置来安装插件。使用以下命令将Capgo技能添加到您的AI工具中:

终端窗口
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

然后使用以下提示:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-photo-library` plugin in my project.

如果您更喜欢手动设置,请运行以下命令并按照以下平台特定的说明进行安装:

终端窗口
bun add @capgo/capacitor-photo-library
bunx cap sync

导入

导入
import { PhotoLibrary } from '@capgo/capacitor-photo-library';

获取当前授权状态,不会提示用户。

import { PhotoLibrary } from '@capgo/capacitor-photo-library';
await PhotoLibrary.checkAuthorization();

如果需要,请求访问相册的权限。

import { PhotoLibrary } from '@capgo/capacitor-photo-library';
await PhotoLibrary.requestAuthorization();

获取可用的相册。

import { PhotoLibrary } from '@capgo/capacitor-photo-library';
await PhotoLibrary.getAlbums();

getLibrary

获取库

获取库资源和可以在 web 视图中显示的 URL。

import { PhotoLibrary } from '@capgo/capacitor-photo-library';
await PhotoLibrary.getLibrary();

getPhotoUrl

获取照片 URL

获取照片资源的显示 URL。 如果您已经调用了 getLibrary with includeFullResolutionData,通常情况下您不需要这个方法。

import { PhotoLibrary } from '@capgo/capacitor-photo-library';
await PhotoLibrary.getPhotoUrl({} as { id: string });

getThumbnailUrl

获取缩略图 URL

获取照片资源的缩略图 URL。

import { PhotoLibrary } from '@capgo/capacitor-photo-library';
await PhotoLibrary.getThumbnailUrl({} as {
id: string;
width?: number;
height?: number;
quality?: number;
});

pickMedia

选择媒体

打开本地系统选择器,用户可以选择媒体而不授予全局照片库访问权限。 选择的文件被复制到应用程序缓存中并返回可移植的URL。

import { PhotoLibrary } from '@capgo/capacitor-photo-library';
await PhotoLibrary.pickMedia();

类型参考

类型参考

PhotoLibraryAuthorizationState

类型参考:PhotoLibraryAuthorizationState
export type PhotoLibraryAuthorizationState = 'authorized' | 'limited' | 'denied' | 'notDetermined';
export interface PhotoLibraryAlbum {
id: string;
title: string;
assetCount: number;
}
export interface GetLibraryOptions {
/**
* Number of assets to skip from the beginning of the query.
*/
offset?: number;
/**
* Maximum number of assets to return. Omit to return everything that matches.
*/
limit?: number;
/**
* Include images in the result. Defaults to `true`.
*/
includeImages?: boolean;
/**
* Include videos in the result. Defaults to `false`.
*/
includeVideos?: boolean;
/**
* Include information about the albums each asset belongs to. Defaults to `false`.
*/
includeAlbumData?: boolean;
/**
* Include assets stored in the cloud (iCloud / Google Photos). Defaults to `true`.
*/
includeCloudData?: boolean;
/**
* If `true`, use the original filenames reported by the OS when available.
*/
useOriginalFileNames?: boolean;
/**
* Width of the generated thumbnails. Defaults to `512`.
*/
thumbnailWidth?: number;
/**
* Height of the generated thumbnails. Defaults to `384`.
*/
thumbnailHeight?: number;
/**
* JPEG quality for generated thumbnails (0-1). Defaults to `0.5`.
*/
thumbnailQuality?: number;
/**
* When `true`, copies the full sized asset into the app cache and returns its URL.
* Defaults to `false`.
*/
includeFullResolutionData?: boolean;
}
export interface GetLibraryResult {
assets: PhotoLibraryAsset[];
/**
* Total number of assets matching the query in the library. `assets.length` can be less
* than this value when pagination is used.
*/
totalCount: number;
/** Whether more assets are available when using pagination. */
hasMore: boolean;
}

PhotoLibraryFile

PhotoLibraryFile
export interface PhotoLibraryFile {
/** Absolute path on the native file system. */
path: string;
/**
* URL that can be used inside a web view. Usually produced by `Capacitor.convertFileSrc(path)`.
*/
webPath: string;
mimeType: string;
/** Size in bytes if known, otherwise `-1`. */
size: number;
}

PickMediaOptions

选择媒体选项
export interface PickMediaOptions {
/**
* Maximum number of items the user can select. Use `0` to allow unlimited selection.
* Defaults to `1`.
*/
selectionLimit?: number;
/** Allow the user to select images. Defaults to `true`. */
includeImages?: boolean;
/** Allow the user to select videos. Defaults to `false`. */
includeVideos?: boolean;
/** Width of the generated thumbnails for picked items. Defaults to `256`. */
thumbnailWidth?: number;
/** Height of the generated thumbnails for picked items. Defaults to `256`. */
thumbnailHeight?: number;
/** JPEG quality for generated thumbnails (0-1). Defaults to `0.7`. */
thumbnailQuality?: number;
}

PickMediaResult

选择媒体结果
export interface PickMediaResult {
assets: PhotoLibraryAsset[];
}

PhotoLibraryAsset

照片库资产
export interface PhotoLibraryAsset {
id: string;
fileName: string;
type: PhotoAssetType;
width: number;
height: number;
duration?: number;
creationDate?: string;
modificationDate?: string;
latitude?: number;
longitude?: number;
mimeType: string;
/** Size in bytes reported by the OS for the underlying asset, if available. */
size?: number;
albumIds?: string[];
thumbnail?: PhotoLibraryFile;
file?: PhotoLibraryFile;
}

PhotoAssetType

照片资产类型
export type PhotoAssetType = 'image' | 'video';

真实来源

真实来源

本页面由插件生成 src/definitions.ts. 当上游的公共 API 变更时,请重新同步

如果您正在使用 Getting Started 来规划仪表板和 API 操作,请将其与 使用 @capgo/capacitor-photo-library 来使用 Using @capgo/capacitor-photo-library 中的本机功能 API Overview 来查看 API Overview 中的实现细节 介绍 在介绍中实现细节 API 键 在 API 键中实现细节, 设备 在设备中实现细节。