Skip to content

Commit

Permalink
Fixed bug that caused photos to not save properly if it was first sav…
Browse files Browse the repository at this point in the history
…e after granting gallery access (#7)
  • Loading branch information
patrykfdt authored Jul 27, 2019
1 parent f6f992a commit e222159
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 61 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.1.3

* Fixed bug that caused photos to not save properly if it was first save after granting gallery access

## 0.1.2

* Fixed bug that caused too many named albums to be created on iOS
Expand Down
26 changes: 14 additions & 12 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,20 @@ class _MyAppState extends State<MyApp> {
final image = NetworkImage(url);
final key = await image.obtainKey(ImageConfiguration());
final load = image.load(key);
load.addListener((listener, err) async {
final byteData =
await listener.image.toByteData(format: ImageByteFormat.png);
final bytes = byteData.buffer.asUint8List();
final res = await _imageSaver.saveImage(
imageBytes: bytes,
directoryName: "dir_name",
);
_stopLoading();
_displayResult(res);
print(res);
});
load.addListener(
ImageStreamListener((listener, err) async {
final byteData =
await listener.image.toByteData(format: ImageByteFormat.png);
final bytes = byteData.buffer.asUint8List();
final res = await _imageSaver.saveImage(
imageBytes: bytes,
directoryName: "dir_name",
);
_stopLoading();
_displayResult(res);
print(res);
}),
);
}

/// Saves one of asset images to gallery
Expand Down
12 changes: 6 additions & 6 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.2.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -73,21 +73,21 @@ packages:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
version: "1.7.0"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "2.0.3"
save_in_gallery:
dependency: "direct dev"
description:
path: ".."
relative: true
source: path
version: "0.1.2"
version: "0.1.3"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -134,7 +134,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.4"
version: "0.2.5"
typed_data:
dependency: transitive
description:
Expand Down
96 changes: 60 additions & 36 deletions ios/Classes/ImageSaver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,47 +25,67 @@ class ImageSaver: NSObject {
imagesRemaining = 1
savingMultipleImages = false

if let dir = dir {
createAlbumIfNeeded(albumName: dir, completion: { assetCollection in
self.save(image, in: assetCollection)
})
} else {
save(image)
}
requestPermissionIfNeeded(completion: { (permissionGranted) in
if permissionGranted {
if let dir = dir {
self.createAlbumIfNeeded(albumName: dir, completion: { assetCollection in
self.save(image, in: assetCollection)
})
} else {
self.save(image)
}
} else {
self.onSave(success: false)
}
})

}

func saveImages(_ images: [UIImage], in dir: String?) {
savingMultipleImages = true
imagesRemaining = images.count

if let dir = dir {
createAlbumIfNeeded(albumName: dir, completion: { assetCollection in
for image in images {
self.save(image, in: assetCollection)

requestPermissionIfNeeded(completion: { (permissionGranted) in
if permissionGranted {
if let dir = dir {
self.createAlbumIfNeeded(albumName: dir, completion: { assetCollection in
for image in images {
self.save(image, in: assetCollection)
}
})
} else {
for image in images {
self.save(image)
}
}
})
} else {
for image in images {
save(image)
} else {
self.onSave(success: false)
}
}
})

}

func saveImages(_ images: [String: UIImage], in dir: String?) {
savingMultipleImages = true
imagesRemaining = images.count

if let dir = dir {
createAlbumIfNeeded(albumName: dir, completion: { assetCollection in
for (_, image) in images {
self.save(image, in: assetCollection)

requestPermissionIfNeeded(completion: { (permissionGranted) in
if permissionGranted {
if let dir = dir {
self.createAlbumIfNeeded(albumName: dir, completion: { assetCollection in
for (_, image) in images {
self.save(image, in: assetCollection)
}
})
} else {
for (_, image) in images {
self.save(image)
}
}
})
} else {
for (_, image) in images {
save(image)
} else {
self.onSave(success: false)
}
}
})
}

// MARK: save in default album
Expand All @@ -80,18 +100,22 @@ class ImageSaver: NSObject {

// MARK: saving in custom named album
private func save(_ image: UIImage, in dir: PHAssetCollection) {
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
PHPhotoLibrary.requestAuthorization({ (status) -> Void in
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
self.onSave(success: false)
} else {
self.saveInCreatedAlbum(image: image, assetCollection: dir)
}
self.saveInCreatedAlbum(image: image, assetCollection: dir)
}

private func requestPermissionIfNeeded(completion: @escaping (Bool) -> Void) {
if !hasPermission() {
PHPhotoLibrary.requestAuthorization({ (status) in
completion(status == PHAuthorizationStatus.authorized)
})
} else {
self.saveInCreatedAlbum(image: image, assetCollection: dir)
completion(true)
}
}

private func hasPermission() -> Bool {
return PHPhotoLibrary.authorizationStatus() == PHAuthorizationStatus.authorized
}

private func saveInCreatedAlbum(image: UIImage, assetCollection: PHAssetCollection) {
PHPhotoLibrary.shared().performChanges({
Expand All @@ -102,7 +126,7 @@ class ImageSaver: NSObject {
albumChangeRequest?.addAssets(enumeration)
}, completionHandler: { (success, error) -> Void in
self.onSave(success: (error == nil && success))
})
})
}

func fetchAssetCollectionForAlbum(albumName: String) -> PHAssetCollection? {
Expand Down
10 changes: 5 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Generated by pub
# See https://www.dartlang.org/tools/pub/glossary#lockfile
# See https://dart.dev/tools/pub/glossary#lockfile
packages:
async:
dependency: transitive
description:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.2.0"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -66,14 +66,14 @@ packages:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.0"
version: "1.7.0"
quiver:
dependency: transitive
description:
name: quiver
url: "https://pub.dartlang.org"
source: hosted
version: "2.0.2"
version: "2.0.3"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -120,7 +120,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.4"
version: "0.2.5"
typed_data:
dependency: transitive
description:
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: save_in_gallery
description: Flutter plugin that allows you to save images in native gallery in both Android and iOS. You can either save them in default album or in named album of your choice.
version: 0.1.2
author: FiveDotTwelve
version: 0.1.3
author: FiveDotTwelve <[email protected]>
homepage: https://github.com/FiveDotTwelve/save_in_gallery
repository: https://github.com/FiveDotTwelve/save_in_gallery

Expand Down

0 comments on commit e222159

Please sign in to comment.