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

Using Apple's ImageIO on iOS 17 cause APNG pictures flicker abnormally #3697

Open
3 tasks
fuguangxin opened this issue Apr 11, 2024 · 17 comments
Open
3 tasks
Labels
apple bug apple's bug cause our framework author's pain ImageIO Anything related to Apple ImageIO codec png PNG image related

Comments

@fuguangxin
Copy link

New Issue Checklist

Issue Info

Info Value
Platform Name e.g. ios
Platform Version e.g. 17.0 +
SDWebImage Version e.g. 5.19.1
Integration Method e.g. cocoapods
Xcode Version e.g. Xcode 15.2
Repro rate e.g. all the time (100%)
Repro with our demo prj e.g. does it happen with our demo project?
Demo project link e.g. link to a demo project that highlights the issue

Issue Description and Steps

YYImage doesn't have this problem。

Behave as follows
https://recite-words.oss-cn-hangzhou.aliyuncs.com/web/tmp/RPReplay_Final1712828721.MP4 
The code is as follows
WX20240411-174017@2x
The apng is as follows
https://recite-words.oss-cn-hangzhou.aliyuncs.com/headframe/e592f78bf9cb0ffde9520e02879da7b1.png

@dreampiggy
Copy link
Contributor

dreampiggy commented Apr 11, 2024

Why not try use SDAnimatedImageView ?

Also, update SD to the latest version and test both on iOS 16(any 16.x) && 17.3+

iOS 17.0-17.2 has a global PNG decode bug, if you test on this version maybe we can not help

But why YYImage can do xxx on PNG

This seems YY does not use ImageIO, it use a copy of libpng (a third part PNG codec) (Maybe because at that time, ImageIO does not support APNG, 7 years ago)

Using third party codec is the last choice, because it can not use Hardware Media Decoder to accelerate, which may consume more CPU/Battery so we usually don't (unless we found something buggy and will provide a custom plugin instead. see readme about plugin system)

@dreampiggy
Copy link
Contributor

tagged as Apple's ImageIO bug currently

@dreampiggy dreampiggy added ImageIO Anything related to Apple ImageIO codec apple bug apple's bug cause our framework author's pain labels Apr 11, 2024
@fuguangxin
Copy link
Author

Why not try use SDAnimatedImageView ?

Also, update SD to the latest version and test both on iOS 16(any 16.x) && 17.3+

iOS 17.0-17.2 has a global PNG decode bug, if you test on this version maybe we can not help

But why YYImage can do xxx on PNG

This seems YY does not use ImageIO, it use a copy of libpng (a third part PNG codec) (Maybe because at that time, ImageIO does not support APNG, 7 years ago)

Using third party codec is the last choice, because it can not use Hardware Media Decoder to accelerate, which may consume more CPU/Battery so we usually don't (unless we found something buggy and will provide a custom plugin instead. see readme about plugin system)

Thanks , I will try. I'm sure iOS 17 has some bug on Apng, because some of my static png in the asset abnormally either.

@dreampiggy
Copy link
Contributor

Reopen if you tested on iOS 17.3 and it's still broken

@dreampiggy
Copy link
Contributor

dreampiggy commented Apr 11, 2024

Anyway, SDWebImage's coder is plugin based

If you don't want to use Apple's ImageIO, OK.

Just see (https://github.com/SDWebImage/SDWebImage/wiki/Coder-Plugin-List) and use libpng(https://github.com/rickytan/libpng-apng) to write a custom coder for APNG, like what YYImage's done.

libpng with APNG is not merged into upstream, many many people fork their own and copy the code from Mozilla 😂, even like https://github.com/onevcat/libpng/

@dreampiggy
Copy link
Contributor

dreampiggy commented Apr 11, 2024

In history there is a talk to whether SDWebImage org should provide a PNG plugin using libpng, as well as a JPEG plugin using libjpeg-turbo (beyond that ImageIO, Apple's close-sourced image codec)

But was rejected because we think Apple's media team is good at it. Integrate another codec is bad for both code size and performance.

See more:

#3605
#2991

However, seems Apple's media team code quality is lower and lower these years🤮

@dreampiggy
Copy link
Contributor

dreampiggy commented Apr 11, 2024

@fuguangxin A stupid way:

Use https://github.com/SDWebImage/SDWebImageYYPlugin

This wrap a YYImageDecoder into SDWebImage's coder plugin API (means, conforms to SDImageCoder protocol)

You can use this coder to force load PNG without Apple's ImageIO if you want.

var context: [SDWebImageContextOption: AnyObject] = [:]
if url.pathExtension == ".png" {
   context[.imageCoder] = SDImageYYCoder.shared // magic here
}
view.sd_setImage(with: url, placeholderImage: nil, options: [], context: context)

or you can wrap it as a global coder well, by override the canDecode(with: Data) and only return true for PNG data.

Have a try, if this repo does not work as expected please ping me (2 years after the last update from me?)

@fuguangxin
Copy link
Author

Reopen if you tested on iOS 17.3 and it's still broken

Yes, it's still broken , my device version is 17.3.1

@fuguangxin fuguangxin reopened this Apr 12, 2024
@fuguangxin
Copy link
Author

What about this ? Use https://github.com/SDWebImage/SDWebImageYYPlugin

Actually, that https://github.com/SDWebImage/SDWebImageYYPlugin/blob/master/SDWebImageYYPlugin/Classes/YYImage/YYImageBridge/SDImageYYCoder.m

It's ok,but need add code like this
[SDImageCodersManager.sharedManager addCoder:SDImageYYCoder.sharedCoder];

@dreampiggy
Copy link
Contributor

dreampiggy commented Apr 12, 2024

So this is concluded as Apple's Bug ?

Should we fix this ? (provide a libpng based codec ?)

or update to #3605

A new bug in iOS 17's ImageIO APNG decoder (not SDWebImage's issue)

@dreampiggy dreampiggy added the png PNG image related label Apr 12, 2024
@dreampiggy dreampiggy changed the title [APNG] The picture flickers abnormally Using Apple's ImageIO on iOS 17 cause some APN picture flickers abnormally Apr 12, 2024
@dreampiggy dreampiggy changed the title Using Apple's ImageIO on iOS 17 cause some APN picture flickers abnormally Using Apple's ImageIO on iOS 17 cause APNG pictures flicker abnormally Apr 12, 2024
@fuguangxin
Copy link
Author

So this is concluded as Apple's Bug ?

Should we fix this ? (provide a libpng based codec ?)

or update to #3605

A new bug in iOS 17's ImageIO APNG decoder (not SDWebImage's issue)

I suggest that we can provide a way to solve this problem.Apple has not solved this problem for too long.

@dreampiggy
Copy link
Contributor

dreampiggy commented Apr 12, 2024

I suggest that we can provide a way to solve this problem.Apple has not solved this problem for too long.

Is there any open-source libpng (A objc/C++/swift wrapper is better) available ?

Must support at least CocoaPods package manager (SwiftPM is better)

Or can you help for some PR ?


Because it's Apple's bug, in most cases, I don't want to waste my time to fix it. Until the fixing cost is low

Only if there are something like https://github.com/SDWebImage/libwebp-Xcode available for APNG, I can spend days to fix.

@awxkee
Copy link

awxkee commented Apr 15, 2024

I suggest that we can provide a way to solve this problem.Apple has not solved this problem for too long.

Is there any open-source libpng (A objc/C++/swift wrapper is better) available ?

Must support at least CocoaPods package manager (SwiftPM is better)

Or can you help for some PR ?

Because it's Apple's bug, in most cases, I don't want to waste my time to fix it. Until the fixing cost is low

Only if there are something like https://github.com/SDWebImage/libwebp-Xcode available for APNG, I can spend days to fix.

It will take some time any way to integrate APNG. APNG stores each frame in different size and expects that you will position and blend next frame into last one, and do not provide integrated method to do so.

Download and apply a APNG patch for libpng and create SPM, or find one, it is easiest part I think:)

@awxkee
Copy link

awxkee commented Apr 15, 2024

Also libpng itself depends on zlib, so link against z is required, but there are known serious vulnerability in zlib before iOS 15.5 and that means compile zlib or zlib-ng and link to it required also to avoid publish with known vulnerabilities.

Perhaps, integrating libpng may not worth it

@dreampiggy
Copy link
Contributor

YYImage do the simple thing to add extensions to apng, why not just continue to use that ?

You can filter only PNG format and iOS 17.0 (means , buggy version you want to support) use that YYImageDecoder

@dreampiggy
Copy link
Contributor

dreampiggy commented Apr 16, 2024

It will take some time any way to integrate APNG. APNG stores each frame in different size and expects that you will position and blend next frame into last one, and do not provide integrated method to do so.

THIS IS ACTUALLY THE EASIEST PART.

Because I already written at least 5 times of this code, like webp avif bpg. Use a CGcontext as canvas is easy: https://github.com/SDWebImage/SDWebImageWebPCoder/blob/fed1583714dc02489ec69debdaf6fafb1dc0581a/SDWebImageWebPCoder/Classes/SDImageWebPCoder.m#L544

But anyway, I still think OS provided codec should handle this, the API (CGImageSourceCreateImageAtIndex) should always return the final one, instead of internal-mixing part of frame

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
apple bug apple's bug cause our framework author's pain ImageIO Anything related to Apple ImageIO codec png PNG image related
Projects
None yet
Development

No branches or pull requests

3 participants