Skip to content
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

[image-picker][ios] Fix Animated GIF support #20034

Merged
merged 8 commits into from
Nov 16, 2022

Conversation

barthap
Copy link
Contributor

@barthap barthap commented Nov 14, 2022

Why

I was talking to @hirbod who discovered that animated gifs aren't working on iOS in contrast to what docs say:

Animated GIFs support If the selected image is an animated GIF, the result image will be an animated GIF too if and only if quality is set to undefined and allowsEditing is set to false. Otherwise compression and/or cropper will pick the first frame of the GIF and return it as the result (on Android the result will be a PNG, on iOS — GIF).

What's more, I tracked it down that they probably never worked before (what I mentioned in #18138 description), because the initial Objective-C implementation was wrong (SDK 44 and earlier). Further Swift PRs (#15977, #18135, #18138) only refactored the already-bad Obj-C implementation.

How

Found a way to do it properly with the CoreGraphics framework:

  • For non-compressed images, it is enough to passthrough the raw data
  • For images with quality set or being cropped, we need to recreate the GIF:
    1. Get source image metadata
    2. Create a destination buffer
    3. Copy image frames, crop / compress each frame if needed
    4. Save destination image data

Also updated the docs accordingly.

Test Plan

NCL on iOS Simulator, animated gif, all combinations of:

  • Quality: undefined, 1.0, !=1.0
  • allowsEditing: true, false

Also output from my test app (the built-in iOS crop preview is buggy 🤷):

Screen.Recording.2022-11-14.at.13.00.23.mov

Discussion topics ❓

Two things:

  • It was undocumented, that on Android (at least SDK 47), quality MUST be set to 1.0 for animated gifs to work. Docs were saying something opposite, that it must be undefined (docs were wrong). My PR fixes that description. Please review my wording there.

  • On iOS, until now it wasn't working anyway. Even though I made it work for all qualities, it behaves the same for quality set to 1.0 or undefined. This is in contrast to JPEG, where quality defaults to 0.2. Should we:

    1. also compress the GIFs by default - when quality is not set, as we do for JPEG
    2. keep them untouched as they're now on iOS - IMO this is better, because compression sometimes acts weirdly and they're larger than uncompressed

    If 1) then I need to adjust this PR to use default quality when nil.
    If 2) then the Android part needs a fix and so the docs accordingly.

@expo-bot expo-bot added the bot: suggestions ExpoBot has some suggestions label Nov 14, 2022
barthap and others added 3 commits November 14, 2022 13:16
Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
@expo-bot expo-bot added bot: passed checks ExpoBot has nothing to complain about and removed bot: suggestions ExpoBot has some suggestions labels Nov 14, 2022
@hirbod
Copy link
Contributor

hirbod commented Nov 14, 2022

Awesome work and thanks for picking this up so quickly.

@barthap barthap marked this pull request as ready for review November 14, 2022 12:40
@barthap barthap requested a review from tsapeta November 14, 2022 12:40
Copy link
Contributor

@lukmccall lukmccall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good ✅

Copy link
Contributor

@lukmccall lukmccall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep them untouched as they're now on iOS - IMO this is better, because compression sometimes acts weirdly and they're larger than uncompressed

I would go with that solution ;)

Copy link
Member

@tsapeta tsapeta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Good to see you back 🤗
I'm also in favor of 2) 😉

packages/expo-image-picker/ios/MediaHandler.swift Outdated Show resolved Hide resolved
Co-authored-by: Tomasz Sapeta <tomasz.sapeta@swmansion.com>
@barthap barthap merged commit 79b0823 into main Nov 16, 2022
@barthap barthap deleted the @barthap/image-picker/ios-gif branch November 16, 2022 06:20
tsapeta added a commit that referenced this pull request Nov 21, 2022
# Why

I was talking to @hirbod who discovered that animated gifs aren't
working on iOS in contrast to [what docs
say](https://docs.expo.dev/versions/latest/sdk/imagepicker/#imagepickerlaunchimagelibraryasyncoptions):
> Animated GIFs support If the selected image is an animated GIF, the
result image will be an animated GIF too if and only if quality is set
to undefined and allowsEditing is set to false. Otherwise compression
and/or cropper will pick the first frame of the GIF and return it as the
result (on Android the result will be a PNG, on iOS — GIF).

What's more, I tracked it down that they probably never worked before
(what I mentioned in #18138 description), because the initial
Objective-C implementation was wrong (SDK 44 and earlier). Further Swift
PRs (#15977, #18135, #18138) only refactored the already-bad Obj-C
implementation.

# How

Found a way to do it properly with the `CoreGraphics` framework:
- For non-compressed images, it is enough to passthrough the raw data
- For images with `quality` set or being cropped, we need to recreate
the GIF:
  1. Get source image metadata
  2. Create a destination buffer
  3. Copy image frames, crop / compress each frame if needed
  4. Save destination image data

Also updated the docs accordingly.

# Test Plan

NCL on iOS Simulator, animated gif, all combinations of:
- Quality: `undefined`, `1.0`, `!=1.0`
- `allowsEditing`: `true`, `false`

Also output from my test app (_the built-in iOS crop preview is buggy
🤷_):


https://user-images.githubusercontent.com/278340/201655161-c09a8f2e-e51e-4ba3-bb5b-b953050d3f2c.mov

# Discussion topics ❓

Two things:

- It was undocumented, that on Android (at least SDK 47), quality MUST
be set to `1.0` for animated gifs to work. Docs were saying something
opposite, that it must be `undefined` (docs were wrong). My PR fixes
that description. Please review my wording there.
- On iOS, until now it wasn't working anyway. Even though I made it work
for all qualities, it behaves the same for quality set to `1.0` or
`undefined`. This is in contrast to JPEG, where quality defaults to
`0.2`. Should we:
1. also compress the GIFs by default - when quality is not set, as we do
for JPEG
2. keep them untouched as they're now on iOS - IMO this is better,
because compression sometimes acts weirdly and they're larger than
uncompressed
  
  If 1) then I need to adjust this PR to use default quality when `nil`.
  If 2) then the Android part needs a fix and so the docs accordingly.

Co-authored-by: Expo Bot <34669131+expo-bot@users.noreply.github.com>
Co-authored-by: Tomasz Sapeta <tomasz.sapeta@swmansion.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot: passed checks ExpoBot has nothing to complain about
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants