fix: use original file when sharing image with alpha channel#343
fix: use original file when sharing image with alpha channel#343
Conversation
Signed-off-by: Bugen Zhao <i@bugenzhao.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes image sharing behavior to preserve the original file format when sharing images with alpha channels. Previously, all eligible images were converted to JPEG, which would lose alpha channel information. The changes ensure that images with transparency are shared in their original format.
Changes:
- Added alpha channel detection to
isPlainImageproperty to exclude images with alpha from JPEG conversion - Added diagnostic logging when transferable image creation fails
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| app/Shared/Models/ViewingImageModel.swift | Adds alpha channel check to isPlainImage to prevent images with transparency from being converted to JPEG |
| app/iOS/Views/NewImageViewer.swift | Adds warning log when TransferableImage initialization fails for debugging purposes |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| var isPlainImage: Bool { | ||
| if sd_isAnimated { return false } | ||
| if cgImage?.alphaInfo ?? .none != .none { return false } |
There was a problem hiding this comment.
The alpha channel detection logic may incorrectly identify images with .noneSkipFirst or .noneSkipLast alpha info as having an alpha channel. According to Apple's documentation, CGImageAlphaInfo has several cases that indicate no alpha channel:
.none: No alpha channel.noneSkipLast: No alpha channel, ignore last component (e.g., RGB_555 format).noneSkipFirst: No alpha channel, ignore first component (e.g., BGR_555 format)
The current check only compares against .none, which means images with .noneSkipFirst or .noneSkipLast would be incorrectly treated as having an alpha channel. This would cause them to be shared as the original file instead of being converted to JPEG, which is unnecessarily conservative.
Consider checking if the alpha info actually represents an alpha channel by using a more comprehensive check. For example:
let hasAlpha = cgImage?.alphaInfo.map { info in
info != .none && info != .noneSkipFirst && info != .noneSkipLast
} ?? false
if hasAlpha { return false }
While this issue affects relatively rare image formats and fails conservatively (preserving quality), it's worth fixing for correctness.
| if cgImage?.alphaInfo ?? .none != .none { return false } | |
| let hasAlpha = cgImage?.alphaInfo.map { info in | |
| info != .none && info != .noneSkipFirst && info != .noneSkipLast | |
| } ?? false | |
| if hasAlpha { return false } |
No description provided.