-
Notifications
You must be signed in to change notification settings - Fork 3
fix: apply targetWidth and targetHeight when picking images from gallery #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ class IONCAMRPhotoLibraryService: NSObject, ObservableObject { | |
| private let mediaTypeArray: [PHAssetMediaType] | ||
| private let thumbnailAsData: Bool | ||
| private let returnMetadata: Bool | ||
| private let targetSize: IONCAMRSize? | ||
|
|
||
| /// The manager that will fetch and cache photos for us | ||
| var imageCachingManager = PHCachingImageManager() | ||
|
|
@@ -31,13 +32,15 @@ class IONCAMRPhotoLibraryService: NSObject, ObservableObject { | |
| metadataGetter: IONCAMRMetadataGetterDelegate, | ||
| mediaTypeArray: [PHAssetMediaType], | ||
| thumbnailAsData: Bool, | ||
| returnMetadata: Bool | ||
| returnMetadata: Bool, | ||
| targetSize: IONCAMRSize? = nil | ||
| ) { | ||
| self.delegate = delegate | ||
| self.metadataGetter = metadataGetter | ||
| self.mediaTypeArray = mediaTypeArray | ||
| self.thumbnailAsData = thumbnailAsData | ||
| self.returnMetadata = returnMetadata | ||
| self.targetSize = targetSize | ||
| super.init() | ||
| PHPhotoLibrary.shared().register(self) | ||
| } | ||
|
|
@@ -154,13 +157,28 @@ extension IONCAMRPhotoLibraryService { | |
|
|
||
| private func fetchImage(from asset: PHAsset) async throws -> IONCAMRMediaResult { | ||
| guard let image = try await fetchImage(byLocalIdentifier: asset.localIdentifier), | ||
| let imageData = image.pictureThumbnailData(), | ||
| let imageURL = try await fetchImageURL(for: asset) | ||
| else { throw IONCAMRError.imageNotFound } | ||
|
|
||
| let finalImage: UIImage | ||
| let imageData: String | ||
| if let size = targetSize { | ||
| let resized = image.resizeTo(CGSize(size: size)) ?? image | ||
| let quality = CGFloat(IONCAMRTakePhotoOptions.ThumbnailDefaultConfigurations.quality) / 100 | ||
| guard let data = resized.jpegData(compressionQuality: quality)?.base64EncodedString() | ||
| else { throw IONCAMRError.imageNotFound } | ||
| finalImage = resized | ||
| imageData = data | ||
|
Comment on lines
+166
to
+171
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wondering if this should be applied to only the thumbnail or also the image URI being returned (i.e. save the image to a new URI, possibly cache, and return that instead)? Also, targetWidth / targetHeight doesn't seem to get applied if we pass something in "editable". Possibly it goes through a different flow? |
||
| } else { | ||
| guard let data = image.pictureThumbnailData() | ||
| else { throw IONCAMRError.imageNotFound } | ||
| finalImage = image | ||
| imageData = data | ||
| } | ||
|
|
||
| var metadata: IONCAMRMetadata? | ||
| if returnMetadata { | ||
| metadata = try? metadataGetter.getImageMetadata(from: image, and: imageURL) | ||
| metadata = try? metadataGetter.getImageMetadata(from: finalImage, and: imageURL) | ||
| } | ||
|
|
||
| return IONCAMRMediaResult(pictureWith: imageURL.absoluteString, imageData, and: metadata) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought the resize would be to the exact dimensions, but it seems that it does it an way that makes it such that it can switch up the dimensions. I think the capacitor example app doesn't show the thumbnail so it makes it hard to tell (related to my other comment), but this logic probably comes from the old cordova plugin / native library, but just checking if it's by design.
Because if it is, maybe we need to see what Android should do, and update the docs on what targetWidth / targetHeight are meant to be?