For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /api/angular-lynx/media/image-picker.md.

LynxImagePicker

Lets someone pick a photo with the phone's photo picker, or take one with its camera, and hands it back as a Blob. import { LynxImagePicker, ImagePickerError, type ImagePickerOptions, type ImagePickerSource, type ImagePickerErrorCode, type PickedImage } from '@blotch/angular-lynx';

Pick a Photo shows it in use.

Turn it on

Picking from the library needs no setting: every app ng build --platform makes has it. Only the camera needs one.

Add permissions.camera to the build target's options in angular.json, with the reason iOS shows when it asks for the camera:

angular.json
"options": {
  "permissions": { "camera": "Take a photo of the receipt" }
}

What it adds:

  • iOS: NSCameraUsageDescription, the reason iOS's camera prompt shows, and the picker's camera code.
  • Android: the CAMERA permission, which Android asks for before the picker opens the camera app, and the picker's camera code. The camera is declared not required, so Google Play still offers the app to devices without one.

It also turns on LynxBarcodeScanner.

Without it, the camera code is left out of both apps, and pick({ source: 'camera' }) rejects with unavailable. App Store Connect flags an app whose code uses the camera without saying why. The library picker is still there.

See Turn on device features for every setting.

Usage

const picker = inject(LynxImagePicker);

const picked = await picker.pick({ maxDimension: 1024, quality: 0.85 });
if (picked) {
  await uploadBytes(storageRef, picked.blob, { contentType: picked.type });
}

API

LynxImagePicker

@Injectable({ providedIn: 'root' })
class LynxImagePicker {}
PlatformLibraryCamera
iOSPHPickerViewControllerUIImagePickerController
AndroidThe Photo PickerThe phone's camera app, after asking for the camera permission
Web previewAn <input type="file" accept="image/*">The same input, with capture="environment"
Lynx ExplorerRejects with unavailableRejects with unavailable
Unit tests with the testing libraryWhat setPickedImage() givesWhat setTakenPhoto() gives

Only the apps --platform builds, with ng build or ng serve, have the native module behind it. The camera is only in apps whose angular.json sets permissions.camera.

Under the testing library, pick() rejects with unavailable until a spec gives it a photo.

LynxImagePicker.pick()

pick(options?: ImagePickerOptions): Promise<PickedImage | null>;

Opens the picker, or the camera with source: 'camera'. Resolves with the photo picked or taken, or with null when the person closes it without choosing. Rejects with an ImagePickerError, or with a RangeError for an option out of range.

One picker can be open at a time. Picking again while one is open:

  • On iOS and Android, rejects with failed.
  • In the web preview, resolves the open pick with null.

LynxImagePicker.recover()

recover(): Promise<PickedImage | null>;

Resolves with the photo the person picked or took while Android restarted the app, which no pick() was left to resolve with, or with null when there's none. Rejects with an ImagePickerError with failed when that photo couldn't be read.

Android can end the app's screen while the picker or the camera is in front, when it needs the memory or with "Don't keep activities" on, and start it again when the person has chosen. The app starts again with it, so the pick() that opened the picker has gone. Call recover() as the app starts:

const lost = await picker.recover();
if (lost) this.attach(lost);
  • It resolves with the photo once, prepared with the maxDimension and quality the lost pick() asked for.
  • It waits while Android still has to hand the restarted app the result, until the picker or the camera closes.
  • It reads the photo only when it's called, so an app that never calls it doesn't read it.
  • A photo nobody asks for isn't handed over after a minute, and is dropped when another picker or camera opens, even if the person cancels it, or when the person closes the app or the app ends.
Platformrecover()
AndroidThe photo picked or taken while Android restarted the app
iOS, web preview, Lynx Explorer, unit testsnull: iOS doesn't end an app's screen while a picker is open

ImagePickerOptions

type ImagePickerOptions = {
  readonly source?: ImagePickerSource;
  readonly maxDimension?: number;
  readonly quality?: number;
};

type ImagePickerSource = 'library' | 'camera';
OptionDescription
sourcelibrary (the default) to pick a photo the person has, or camera to take a new one, which needs a setting
maxDimensionScales the photo down, if it's larger, so its longest side has at most this many pixels, at least 1. It's never scaled up
qualityThe JPEG quality of the re-encoded photo, from 0 to 1. Defaults to 0.9

Either option re-encodes the photo as a JPEG:

  • upright, whichever way the camera stored it
  • without the original's metadata
  • with a white background where it was transparent

Without them, a JPEG, PNG, GIF or WebP comes back as it was picked. Any other format, such as HEIC, is re-encoded as a JPEG at full size.

A photo from the camera is always re-encoded as a JPEG.

PickedImage

type PickedImage = {
  readonly blob: Blob;
  readonly name: string;
  readonly type: string;
};
PropertyDescription
blobThe photo's bytes
nameA file name for it, such as IMG_0042.jpg, whose extension matches type
typeIts MIME type, such as image/jpeg: the same as blob.type
  • On Android, name is the photo's number in the library, such as 1000000020.jpg: the Photo Picker doesn't share the file's own name.
  • A photo from the camera is named for when it was taken, such as IMG_20261129_142305.jpg.

ImagePickerError

class ImagePickerError extends Error {
  readonly code: ImagePickerErrorCode;
}

type ImagePickerErrorCode = 'unavailable' | 'denied' | 'failed';
codeWhen
unavailableThe host has no photo picker. For the camera, also an app without permissions.camera, and a device without a camera, such as the iOS Simulator
deniedThe person didn't let the app use the camera
failedThe picker couldn't open, one was already open, or the picked photo couldn't be read or decoded