runtime
Package runtime is the Go API of the Drift image picker plugin (github.com/go-drift/drift/plugins/imagepicker/plugin in drift.yaml): a photo from the camera, or images from the photo library.
import imagepicker "github.com/go-drift/drift/plugins/imagepicker/runtime"
go func() {
images, err := imagepicker.Pick(ctx, imagepicker.PickOptions{})
if err != nil || len(images) == 0 {
return // failed, or the user canceled
}
drift.Dispatch(func() { s.showImage(images[0].Path) })
}()
Capture and Pick wait for the user, so call them from a goroutine: from the UI thread they fail with platform.ErrBlocksUIThread. One picker is open at a time.
Decode an Image's file with Decode from github.com/go-drift/drift/pkg/image, not the standard library's image.Decode: photos are stored as the camera sensor saw them, with an EXIF orientation that turns them upright, which only the former applies.
Variables
var (
// ErrBusy indicates a picker is already open.
ErrBusy = errors.New("an image picker is already open")
// ErrCameraUnavailable indicates the device has no camera (or no app
// to take photos), such as the iOS Simulator.
ErrCameraUnavailable = errors.New("no camera available")
// ErrCameraNotConfigured indicates Capture on iOS without the plugin's
// camera: config, which declares the camera usage string.
ErrCameraNotConfigured = errors.New("taking photos needs the imagepicker plugin's camera: config")
// ErrPermissionDenied indicates the user denied camera access.
ErrPermissionDenied = errors.New("camera permission denied")
// ErrNoActivity indicates the app has no screen to show a picker over
// (Android, before the first Activity or after the last).
ErrNoActivity = errors.New("no screen to show the picker over")
)
type CaptureOptions
CaptureOptions configures Capture.
type CaptureOptions struct {
// FrontCamera opens the front camera. On Android it is a hint that
// not every camera app honours.
FrontCamera bool
}
type Image
Image is a picked or captured image, copied to a file the app owns in its cache directory. Move it elsewhere to keep it.
type Image struct {
// Path is the image file's absolute path.
Path string
// MimeType is the file's type, always one github.com/go-drift/drift/pkg/image
// Decode reads: "image/jpeg", "image/png", "image/gif" or "image/webp".
// iOS converts every image to JPEG; Android converts a pick in any other
// format (HEIC, AVIF) to JPEG, scaled down if its decoded pixels would
// outgrow the app's memory class (ActivityManager.getMemoryClass).
MimeType string
// Width and Height are the image's size in pixels as displayed, after
// its EXIF orientation: the bounds github.com/go-drift/drift/pkg/image
// Decode returns for it.
Width, Height int
// Size is the file size in bytes.
Size int64
}
func Capture
func Capture(ctx context.Context, opts CaptureOptions) (*Image, error)
Capture opens the camera and returns the photo taken, or nil if the user canceled. It waits for the user, so call it from a goroutine.
func Pick
func Pick(ctx context.Context, opts PickOptions) ([]Image, error)
Pick opens the photo library and returns the images picked, none if the user canceled. It waits for the user, so call it from a goroutine.
type PickOptions
PickOptions configures Pick.
type PickOptions struct {
// Multiple lets the user pick several images.
Multiple bool
}
Generated by gomarkdoc