快速入门
-
安装插件
Terminal window npm i @capgo/capacitor-photo-libraryTerminal window pnpm add @capgo/capacitor-photo-libraryTerminal window yarn add @capgo/capacitor-photo-libraryTerminal window bun add @capgo/capacitor-photo-library -
同步平台
Terminal window npx cap syncTerminal window pnpm cap syncTerminal window yarn cap syncTerminal window bunx cap sync
- iOS: 在
Info.plist中添加NSPhotoLibraryUsageDescription(如果您计划保存媒体,还需添加NSPhotoLibraryAddUsageDescription)。 - Android: 对于 Android 13+,声明照片/视频权限(
READ_MEDIA_IMAGES、READ_MEDIA_VIDEO)。Capacitor 5 会自动处理这些权限,但请仔细检查自定义清单。
请求访问权限
Section titled “请求访问权限”import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const { state } = await PhotoLibrary.requestAuthorization();if (state !== 'authorized' && state !== 'limited') { throw new Error('照片访问被拒绝');}import { Capacitor } from '@capacitor/core';
const { assets, hasMore, nextPage } = await PhotoLibrary.getLibrary({ limit: 100, includeAlbums: false, thumbnailWidth: 256, thumbnailHeight: 256,});
const gallery = assets.map((asset) => ({ id: asset.id, fileName: asset.fileName, created: asset.creationDate, thumbnailUrl: asset.thumbnail ? asset.thumbnail.webPath ?? Capacitor.convertFileSrc(asset.thumbnail.path) : undefined, photoUrl: asset.file ? asset.file.webPath ?? Capacitor.convertFileSrc(asset.file.path) : undefined,}));要获取其他页面,请调用 PhotoLibrary.getLibrary({ cursor: nextPage })。
让用户选择媒体
Section titled “让用户选择媒体”const selection = await PhotoLibrary.pickMedia({ selectionLimit: 5, includeVideos: true,});
console.log('用户选择了', selection.assets.length, '项');清理缓存文件
Section titled “清理缓存文件”生成的缩略图和导出文件存储在 photoLibrary 下的应用缓存目录中。当您需要回收存储空间时,可以使用您首选的文件 API 删除它们。