Sdwebimage Source Analysis--Loading GIF images

Source: Internet
Author: User

n approaching, immediately holiday, finally put the things at hand, released 3 consecutive apps, I was drunk.

Finally have a little time. Want to study the next sdwebimage how to load gif pictures.

have been very curious.

Start now.

1, first we look at how the Sdwebimage is loaded GIF.

Facebutton.image = [UIImage sd_animatedgifnamed:[nsstring stringwithformat:@ "chata_%d", i-46]];

Sd_animatedgifnamed is a way for sdwebimage to load GIF images. Let's go in this way to see the following.

2,sd_animatedgifnamed This method is implemented as follows. Generates a UIImage object.


+ (UIImage *) sd_animatedgifnamed: (NSString *) name {//Fetch to screen resolution cgfloat scale = [UIScreen mainscreen].scale; Whether it is HD screen if (Scale > 1.0f) {//If it is HD screen take @2x picture//Read picture NSString *retinapath = [[NSBundle mainbund        Le] pathforresource:[name stringbyappendingstring:@ "@2x"] oftype:@ "gif"];        The image is converted to data nsdata *data = [NSData Datawithcontentsoffile:retinapath]; If the picture exists if (data) {//Call Sd_animatedgifwithdata Generate image instance//return [UIImage Sd_an        Imatedgifwithdata:data];                }//If @2x picture does not exist read normal picture NSString *path = [[NSBundle mainbundle] pathforresource:name oftype:@ "gif"];        The image is converted to data data = [NSData Datawithcontentsoffile:path]; If the picture exists if (data) {//Call Sd_animatedgifwithdata to generate an image instance return [UIImage SD_ANIMATEDGIFWITHD        Ata:data];    }//If the picture does not exist return [UIImage Imagenamed:name];    } else {//If the HD screen is not reading the normal picture    NSString *path = [[NSBundle mainbundle] pathforresource:name oftype:@ "gif"];        The image is converted to data nsdata *data = [NSData Datawithcontentsoffile:path]; If the picture exists if (data) {//Call Sd_animatedgifwithdata to generate an image instance return [UIImage SD_ANIMATEDGIFWITHD        Ata:data];    }//If the picture does not exist return [UIImage Imagenamed:name]; }}

The comments are detailed, and this class method mainly determines the resolution of the current device in order to load images of different resolutions.
And then through
Datawithcontentsoffile
Methods the image is converted to NSData to determine whether the NSData exists.
If there is a call to Sd_animatedgifwithdata post-processing.

3,sd_animatedgifwithdata method.


+ (UIImage *) Sd_animatedgifwithdata: (NSData *) data {if (!data) {return nil;    } cgimagesourceref Source = Cgimagesourcecreatewithdata ((__bridge cfdataref) data, NULL);    size_t count = cgimagesourcegetcount (source);    UIImage *animatedimage;    if (count <= 1) {animatedimage = [[UIImage alloc] initwithdata:data];        } else {Nsmutablearray *images = [Nsmutablearray array];        Nstimeinterval duration = 0.0f;            for (size_t i = 0; i < count; i++) {Cgimageref image = Cgimagesourcecreateimageatindex (source, I, NULL);            Duration + = [self 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;    } animatedimage = [UIImage animatedimagewithimages:images duration:duration]; } cfreleasE (source); return animatedimage;}


First look at this line of code
Cgimagesourceref Source = Cgimagesourcecreatewithdata ((__bridge cfdataref) data, NULL);
CGIMAGESOURCEREF is defined as follows,

typedef struct CGIMAGESOURCE *cgimagesourceref;

You can see that it is a cgimagesource pointer.

What is Cgimagesource again?

Cgimagesource is the abstraction of the image data reading task, which can obtain the attributes of image object, thumbnail image and image (including EXIF information).

Then this line of code can be understood as follows: The Nadata takes a series of information from the image.

Goon

size_t count = cgimagesourcegetcount (source);

This line of code is read Cgimagesourceref there are several picture objects.

Next, it's not hard to understand,

Cgimagesourcecreateimageatindex from

The source reads each image into an array.

Reads the time the picture was displayed.

Duration + = [self framedurationatindex:i source:source];

4, calculate the picture display time

+ (float) Framedurationatindex: (nsuinteger) Index Source: (cgimagesourceref) source {    float frameduration = 0.1f;    Cfdictionaryref cfframeproperties = cgimagesourcecopypropertiesatindex (source, index, nil);    Nsdictionary *frameproperties = (__bridge nsdictionary *) cfframeproperties;    Nsdictionary *gifproperties = frameproperties[(NSString *) kcgimagepropertygifdictionary];    NSNumber *delaytimeunclampedprop = gifproperties[(NSString *) kcgimagepropertygifunclampeddelaytime];    if (delaytimeunclampedprop) {        frameduration = [Delaytimeunclampedprop floatvalue];    }    else {        NSNumber *delaytimeprop = gifproperties[(NSString *) kcgimagepropertygifdelaytime];        if (delaytimeprop) {            frameduration = [Delaytimeprop floatvalue];        }    }    if (Frameduration < 0.011f) {        frameduration = 0.100f;    }    Cfrelease (cfframeproperties);    return frameduration;}

Detailed analysis to be continued!!!
5,

Animatedimage = [UIImage animatedimagewithimages:images duration:duration];

Play the image inside the array.

over!!!!








Sdwebimage Source Analysis--Loading GIF images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.