Zum Inhalt springen

Erste Schritte

  1. Plugin installieren

    Terminal-Fenster
    npm i @capgo/capacitor-photo-library
  2. Plattformen synchronisieren

    Terminal-Fenster
    npx cap sync
  • iOS: Info.plist mit NSPhotoLibraryUsageDescription aktualisieren (und NSPhotoLibraryAddUsageDescription, wenn Sie Medien speichern möchten).
  • Android: Für Android 13+ die Foto-/Videoberechtigungen deklarieren (READ_MEDIA_IMAGES, READ_MEDIA_VIDEO). Capacitor 5 behandelt dies automatisch, aber überprüfen Sie benutzerdefinierte Manifeste doppelt.
import { PhotoLibrary } from '@capgo/capacitor-photo-library';
const { state } = await PhotoLibrary.requestAuthorization();
if (state !== 'authorized' && state !== 'limited') {
throw new Error('Fotozugriff verweigert');
}
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,
}));

Um zusätzliche Seiten abzurufen, rufen Sie PhotoLibrary.getLibrary({ cursor: nextPage }) auf.

const selection = await PhotoLibrary.pickMedia({
selectionLimit: 5,
includeVideos: true,
});
console.log('Benutzer hat ausgewählt', selection.assets.length, 'Elemente');

Generierte Miniaturansichten und Exporte werden im App-Cache-Verzeichnis unter photoLibrary gespeichert. Entfernen Sie sie mit Ihrer bevorzugten Datei-API, wenn Sie Speicherplatz zurückgewinnen müssen.