Sdwebimage set cache management, asynchronous download, download times control and optimization functions in one, is basically the iOS developed a standard component. However, in the actual development process, it is found that the support of Sdwebimage to animated GIF is not satisfactory. sample pictures are as follows:
Using Sdwebimage to load the display is much slower than the actual effect
cause Analysis
In Sdwebimage's core file there is a gif extension of uiimage, in which the program gets the image of each frame of GIF and the corresponding display time
for (size_t i = 0; i < count; i++) {
//Get GIF each frame image
cgimageref images = cgimagesourcecreateimageatindex (source, I, NULL);
if (!image) {
continue;
}
Sd_framedurationatindex Gets the display time of each frame image
duration + = [self sd_framedurationatindex:i source:source];
[Images addobject:[uiimage imagewithcgimage:image scale:[uiscreen mainscreen].scale Orientation: Uiimageorientationup]];
Cgimagerelease (image);
}
if (!duration) {
duration = (1.0f/10.0f) * count;
}
Create a dynamic diagram
animatedimage = [UIImage animatedimagewithimages:images duration:duration];
The problem is that when the author obtains the display time of each frame, the purpose is only to compute the total time length of the GIF animation, and does not assign the corresponding weights to the display time of each frame, which results in the average time of each frame image, and brings the cotton effect solution to the person visually.
The solution is actually very simple, because the method provided by the system
+ (UIImage *) Animatedimagewithimages: (nsarray<uiimage *> *) images Duration: (nstimeinterval) duration
Will split the duration into each frame image, in order to achieve the effect of weight distribution, you can increase the number of frames that display time long pictures
There's a mayoff/uiimage-from-animated-gif on the GitHub, that's the principle.
We replace the Sd_animatedgifwithdata method of the Sdwebimage uiimage+gif.m file with the new method
Lazy classmate point here to download the replacement of good files uiimage+gif.m
If you put aside the reliance on sdwebimage, there are other GIF display methods, no longer one by one repeat