💬 혹시 fromm이나 버블 앱을 아십니까...?
해당 앱에서 스타(...)가 보내주는 사진을 저장하면 갤러리에 앱이름의 앨범이 생성되고 해당 앨범에 추가가 되는 것을 볼 수 있다.
이처럼 ios에서 사진 저장시 앱 앨범을 만들고 해당 앨범에 사진을 모아주는 기능을 구현해보았다!
🧐 사용 패키지 :
https://pub.dev/packages/photo_manager
flutter의 좋은 점은 패키지 설명이 잘되어있는 것이 많다는 것이다.
기능 구현 이전에 패키지 사용 설정은 패키지의 readme를 읽어보면 쉽게 이용이 가능하다.
패키지 설명에도 잘 작성되어 있지만,
AssetEntity - 사진(or 동영상) 파일
앨범의 사진을 가져오기 위해서는 페이지 단위로 AssetEntity 리스트를 읽어오거나, range로 리스트를 읽어올 수 있다.
final List<AssetEntity> entities = await path.getAssetListPaged(page: 0, size: 80);
final List<AssetEntity> entities = await path.getAssetListRange(start: 0, end: 80);
우선 갤러리에 접근을 위해 권한 필요
final PermissionState ps = await PhotoManager.requestPermissionExtend();
List<String> _flist = [];// the method can use optional param `permission`.
if (ps.isAuth) {
// Granted
// You can to get assets here.
photoPermission = true;
} else if (ps.hasAccess) {
// Access will continue, but the amount visible depends on the user's selection.
} else {
// Limited(iOS) or Rejected, use `==` for more precise judgements.
// You can call `PhotoManager.openSetting()` to open settings for further steps.
}
void isAlbumExist() async {
final assetsPathList = await PhotoManager.getAssetPathList();
assetsPathList.forEach((element) { if(element.name=="${앨범 명}") {
print(element.id);
albumExist = true;
albumPath = element;
}});
}
void saveToGallery(){
if(Platform.isIOS){
var appDocDir = await getTemporaryDirectory();
//my앨범에 생성됨
isAlbumExist();
if(!albumExist){
albumPath = await PhotoManager.editor.darwin.createAlbum(
"${앨범 명}",
parent: null, // The value should be null, the root path or other accessible folders.
);
}
//비디오 저장. 혹은 사진 저장
var asset = await PhotoManager.editor.saveVideo(저장할 File, title: ${제목});
final AssetEntity? newEntity = await PhotoManager.editor.copyAssetToPath(
asset: asset!,
pathEntity: albumPath!,
);
// 우리의 특정 앨범으로 카피하기 !
final AssetEntity? newEntity = await PhotoManager.editor.copyAssetToPath(
asset: asset!,
pathEntity: albumPath!,
);
}
}
void readFromGalley() async {
List<String> _flist = [];
//앨범의 항목 갯수
final int count = await albumPath!.assetCountAsync;
//포토 갤러리에서 불러오기
final List<AssetEntity> entities = await albumPath!.getAssetListRange(start: 0, end: count);
for(AssetEntity entity in entities){
// entity 처리
}
}