快速入门
复制包含安装步骤和该插件完整 Markdown 指南的配置 prompt。
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-photo-library`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/docs/plugins/photo-library/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
安装
“安装”章节你可以使用我们的 AI 辅助设置来安装插件。使用以下命令将 Capgo skills 添加到你的 AI 工具中:
bunx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins然后使用以下 prompt:
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-librarybunx cap sync导入
“导入”章节import { PhotoLibrary } from '@capgo/capacitor-photo-library';快速入门
章节标题为“快速入门”import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.checkAuthorization();console.log(result);示例:requestAuthorization
标题为“示例:requestAuthorization”的章节import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.requestAuthorization();console.log(result);示例:getAlbums
标题为“示例:getAlbums”的章节import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getAlbums();console.log(result);常见坑点
章节标题为“常见陷阱”- 原生同步:运行
bunx cap sync在插件升级后。 - 唯一可信来源:信任
src/definitions.ts在 GitHub 仓库中。
API 概览
章节标题:“API 概览”checkAuthorization
“checkAuthorization” 章节返回当前授权状态,不会向用户弹出提示。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.checkAuthorization();console.log(result);requestAuthorization
“requestAuthorization” 部分在需要时请求访问照片库。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.requestAuthorization();console.log(result);getAlbums
标题为“getAlbums”的章节获取可用的相册。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getAlbums();console.log(result);getLibrary
标题为“getLibrary”的章节获取库中的资源及其可在 WebView 中显示的 URL。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getLibrary();console.log(result);getPhotoUrl
标题为“getPhotoUrl”的章节获取资源全分辨率版本的显示 URL。
如果您已经调用了 getLibrary 配合 includeFullResolutionData,通常不需要此方法。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getPhotoUrl({ id: 'id-123' });console.log(result);getThumbnailUrl
标题为 “getThumbnailUrl” 的章节获取该资源缩放后缩略图的可显示 URL。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.getThumbnailUrl({ id: 'id-123' });console.log(result);pickMedia
“pickMedia” 章节打开原生系统选择器,让用户无需授予完整照片库权限即可选择媒体。 所选文件会被复制到应用缓存中,并返回可移植的 URL。
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const result = await PhotoLibrary.pickMedia();console.log(result);类型参考
“类型参考” 章节PhotoLibraryAuthorizationState
标题为 “PhotoLibraryAuthorizationState” 的章节export type PhotoLibraryAuthorizationState = 'authorized' | 'limited' | 'denied' | 'notDetermined';PhotoLibraryAlbum
标题为 “PhotoLibraryAlbum” 的章节export interface PhotoLibraryAlbum { id: string; title: string; assetCount: number;}GetLibraryOptions
“GetLibraryOptions” 部分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;}GetLibraryResult
“GetLibraryResult” 部分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
“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
“PickMediaResult” 部分export interface PickMediaResult { assets: PhotoLibraryAsset[];}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
章节标题为“PhotoAssetType”export type PhotoAssetType = 'image' | 'video';单一事实来源
标题为“单一事实来源”的部分本页面由插件的 src/definitions.ts生成。当上游的公共 API 发生变化时,请重新运行 sync。
从入门指南继续
“从入门指南继续”章节如果你正在使用 快速入门 要规划 dashboard 和 API 操作,请将其连接到 使用 @capgo/capacitor-photo-library 在“使用 @capgo/capacitor-photo-library”中的原生功能 API 概览 关于 API Overview 中的实现细节, 简介 关于 Introduction 中的实现细节, API 密钥 有关 API Keys 的实现细节,以及 设备 有关 Devices 的实现细节。