A Flutter native plugin for photo library.
photos_native
is high performance, yet easy to use photo library plugin, working on Android and iOS. It can load albums, load image/thumnail, save, share... photo images.
iOS | Android |
---|---|
-
With Flutter:
$ flutter pub add photos_native
-
YAML
dependencies: photos_native: ^0.0.1
This library depends on flutter-permission-handler, so you need to set up correct permission before using. While the permissions are being requested during runtime, you'll still need to tell the OS which permissions your app might potentially use. That requires adding permission configuration to Android- and iOS-specific files.
Android
You don't need to setup anything if you target SDK 30
or above.
If your compileSdkVersion
/targetSdkVersion
is 29
, you can consider adding android:requestLegacyExternalStorage="true"
to your AndroidManifest.xml
in order to obtain resources:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.your_package">
<application android:label="{appName}"
android:icon="@mipmap/ic_launcher"
android:requestLegacyExternalStorage="true">
</application>
</manifest>
iOS
1. Add the following to your `Podfile` file:post_install do |installer|
installer.pods_project.targets.each do |target|
... # Here are some configurations automatically generated by flutter
# Start of the permission_handler configuration
target.build_configurations.each do |config|
config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
'$(inherited)',
## dart: PermissionGroup.photos
'PERMISSION_PHOTOS=1',
]
end
# End of the permission_handler configuration
end
end
- Config plist file
Add key
NSPhotoLibraryUsageDescription
to the theios/Runner/Info.plist
:
<key>NSPhotoLibraryUsageDescription</key>
<string>App needs access to photo library</string>
PHItem is a photo item on device, PHItem has an photo ID exclusively or file URI.
PHAlbum is abstract version of MediaStore
bucket on Android, or PHAssetCollection
on iOS. PHAlbum can contains multiple PHItems.
PHGallery contains all photo albums on the device.
Default album is All Photos
which contains all photos in gallery (user can select any name to default albums)
PHImageDescriptor is a image descriptor, PHImageDescriptor contain pixel bytes, width and height of the image.
Request permission for photo library
await PhotosNative.requestPermissions();
final gallery = await PhotosNative.loadGallery(title: 'All Photos');
See PHGallery for more details
Return thumbnail image descriptor
final descriptor = await PhotosNative.getThumbnail(200, 200, 'id');
// or load from uri
final descriptor = await PhotosNative.getThumbnail(200, 200, 'image uri');
See class PHImageDescriptor for more details
Return image descriptor of a photo
final descriptor = await PhotosNative.getPixels('photo_id');
See class PHImageDescriptor for more details
Return the number of deleted images
await PhotosNative.delete(['photo_id', 'photo_id2']);
Save image to a path/directory ...
await PhotosNative.save(bytes, width, height, path: path);
Share image with other apps
await PhotosNative.share(bytes, width, height);
Working with Flutter backend texture. Read Flutter Texture for more details. You can experiment these features to check if it can improve your app performance.
int textureId = await PhotosNative.acquireTexture(id, width, height);
// usage
Container(child: Texture(textureId: textureId));
await PhotosNative.releaseTexture(id);
This photo plugin for Flutter is developed by Annotium 👈. Contact us at email