Sdwebimage source Reading (c) uiimage+gif

Source: Internet
Author: User
Tags bitset

Uiimage+gif is a GIF classification of the UIImage class, and in previous versions this category was a bug that dealt with animated GIF images but there was a burst of memory. In the current ' 4.0.0-beta2 ' version of GIF animated image processing is placed in the Uiimage+multiformat this category, and the current GIF classification of the function is only the dynamic image as a static picture to deal with, if it is a static picture of the NSData Data is converted to static UIImage direct return, if it is a dynamic picture of the NSData data, then the image of the 1th frame image is converted to a static uiimage return.

First look at the Uiimage+gif.h file:

1 @interface UIImage (GIF)2 3 /**4 * Compatibility method-creates an animated UIImage from an nsdata, it'll only contain the 1st frame image5  */6+ (UIImage *) Sd_animatedgifwithdata: (NSData *) data;7 8 /**9 * Checks If an UIImage instance is a GIF. Would use the ' images ' arrayTen  */ One-(BOOL) isgif; A  -@end

Defines an instance method and a class method:

1 + (UIImage *) Sd_animatedgifwithdata: (NSData *) data;

A compatible method creates an animated uiimage from a NSData, which contains only the first frame of the image.

Determine if an UIImage instance is a GIF image:

1 -(BOOL) isgif;
1 - (BOOL) isgif {2     return (self.images! = nil); 3 }

  Checks if an instance of UIImage is a GIF and will be judged using an array of "images".

UIIMAGE+GIF.M file:

This paper mainly studies the implementation of the above class methods.

Before learning to study, do some expansion:

size_t Type:

The size_t type is defined in the Cstddef header file, which is the C + + version of the header file stddef.h of the standard library. It is a machine-related unsigned type that is large enough to guarantee the size of the objects in memory.

For example: the size operation of Bitset returns the number of bits in the Bitset object, and the return value type is size_t.

For example, when an element is accessed with subscript, the vector uses vector:size_type as the subscript type, and the correct type of the array subscript is size_t. Vector used by the subscript is actually size_t, source code is: typedef size_t SIZE_TYPE. Source:

SIZE_T is defined in the standard C library and should be unsigned int, which is a long unsigned int in a 64-bit system.

  Use:

A basic, non-signed integer, C + + type, which is the result type returned by the sizeof operator, and the size of the type is selectable. Therefore, it is possible to store the maximum size of any type of array in theory.  In other words, a pointer can be safely put into the size_t type (an exception is the function pointer of the class, but this is a special case). size_t types are typically used for looping, array indexing, storage of size, and address arithmetic. Although size_t can store a pointer, it is designed to better use another unsinged integer type uintptr_t form.  In some cases, it is more efficient to use the size_t type, which is more secure than a program that habitually uses unsigned types. The size_t is defined in a standard library of C + + based on the unsigned integer memsize type. In the C language, this type is located in the header file Stddef.h, and in C + +, in cstddef.h.

  Implementation method:

In C + +, the design of size_t is designed to accommodate multiple platforms. The introduction of size_t enhances the portability of programs on different platforms.

size_t is a type of data that is customized for the system and is generally integral, because the C + + standard defines only the lowest number of digits, not the required number of fixed digits. And in memory, the logarithm of high-aligned storage or low-aligned storage systems are different. To improve the portability of your code, it is necessary to define such a data type. Generally this type will be defined to its specific memory and so on. Of course, some of them are compilers or systems that have been defined well.

It has been tested that size_t is 4 bytes in a 32-bit system, whereas in a 64-bit system, size_t is 8 bytes, so using this type can enhance the portability of the program.

  Detailed Explanation:

  size_t in the C language. It is an "integer" type, which holds an integer, just like int, long.  This integer is used to log a size (size), and the full name of the size_t should be a size type, which means "a data type used to record the size". with sizeof (XXX) operation can get the memory space of xxx occupied bytes, sizeof return must be unsigned shaping, in standard C through typedef the return value type is defined as size_t.  Strlen Gets the number of bytes actually used by the character array, does not contain the array end character ' \ s ', and returns the type size_t.  If you use printf to output the size_t type, C99 defines the format character%zd, if the compiler does not support you can try%u or%lu.  Because the size_t type of data actually holds an integer, it can also do subtraction, or it can be converted to int and assigned to a variable of type int. There are similar wchar_t, ptrdiff_t. wchar_t is the wide char type, "a data type used to record a wide character."  ptrdiff_t is pointer difference type, "a data type used to record the distance between two pointers." In general, size_t and ptrdiff_t are implemented with typedef. You might find a similar statement in a header file: typedef unsigned int size_t;  and wchar_t slightly different.  In some of the older compilers, wchar_t may also be implemented with TypeDef, but the new standard wchar_t is already a C + + language keyword, the status of wchar_t type has been the same as char, int status.  In the standard C/s + + syntax, only the basic data types such as int float char bool, and size_t or size_type are some of the easy-to-understand variant types defined by the basic data types for the convenience of memory. For example: typedef int size_t; Defines the size_t as an integral type.

The size_t type a value will be compiled in the process of compiling his complement. Therefore, in the process of using size_t type data, it is particularly important to note that this type is used in logical expressions, and a little attention may have serious consequences.
Note: positive complement: the same as the original code, the complement of negative numbers: The sign bit is 1, the remaining bits for the absolute value of the original code bitwise negation, and then the entire number plus 1.

Reference Link: http://blog.csdn.net/JIEJINQUANIL/article/details/50981834

Http://jeremybai.github.io/blog/2014/09/10/size-t

Http://baike.baidu.com/link?url=WhKafqypjk2GpAfTxbeWLNEQIqW3bjODmV5faNTtZUcPPCn1Y7MDOa05UR4DOstvaVcbGeOXqUcKCRJ6eIz4ba

http://blog.csdn.net/zhanghaotian2011/article/details/7974891

Http://www.cnblogs.com/kaituorensheng/p/3239446.html

http://blog.csdn.net/bigloomy/article/details/6563870

See below + (UIImage *) Sd_animatedgifwithdata: (NSData *) data; Method Implementation:

1+ (UIImage *) Sd_animatedgifwithdata: (NSData *) Data {2     if(!data) {3         returnNil;4     }5 6Cgimagesourceref Source =Cgimagesourcecreatewithdata ((__bridge cfdataref) data, NULL);7 8size_t count =Cgimagesourcegetcount (source);9 TenUIImage *Staticimage; One  A     if(Count <=1) { -Staticimage =[[UIImage alloc] initwithdata:data]; -}Else { the         //We'll only have retrieve the 1st frame. The full GIF is available via the Flanimatedimageview category. -         //This are only code to allow drawing animated images as static ones - #ifSd_watch -CGFloat scale =1; +Scale =[Wkinterfacedevice Currentdevice].screenscale; - #elifSd_uikit +CGFloat scale =1; AScale =[UIScreen Mainscreen].scale; at #endif -          -Cgimageref cgimage = Cgimagesourcecreateimageatindex (source,0, NULL); - #ifSd_uikit | | Sd_watch -UIImage *frameimage =[UIImage imagewithcgimage:cgimage scale:scale orientation:uiimageorientationup]; -Staticimage = [UIImage animatedimagewithimages:@[frameimage] Duration:0.0f]; in #elifSd_mac -Staticimage =[[UIImage alloc] initwithcgimage:cgimage size:nszerosize]; to #endif + cgimagerelease (cgimage); -     } the  * cfrelease (source); $ Panax Notoginseng     returnStaticimage; -}

The main use of the <ImageIO/CGImageSource.h>in the ImageIO Framework, and in the uiimage+gif.m file started through the # Import <ImageIO/ImageIO.h>, introducing the ImageIO framework.

1. Determine if the incoming data is nil and return nil directly.

2. Using CGImageSource.h: Cgimagesourcecreatewithdata ((__bridge cfdataref) data, NULL), create a Cgimagesourceref object source.

1 /* Create An image of source reading from ' data '.  The ' Options ' dictionary2  * May is used to request additional creation options; see the list of KEYS3*/45 imageio_extern cgimagesourceref __nullable Cgimagesourcecreatewithdata (cfdataref __nonnull data, cfdictionaryref __nullable options) imageio_available_starting (__mac_10_4, __IPHONE_4_0);

3. Using CGImageSource.h: Cgimagesourcegetcount (source), get the number of images inside the source count.

1 /* Return the number of images (not including thumbnails) in the image 2  */34 imageio_extern size_t cgimagesourcegetcount (cgimagesourceref __nonnull ISRC)  imageio_available_starting (__mac_10_4, __IPHONE_4_0);

4. Create a UIImage instance staticimage. (It is possible to guess the intention of creating the instance from the name)

5. If count is less than or equal to 1, that is, the data is a static picture of the NSData data, then convert data to UIImage and assigned to Staticimage.

6. If count is greater than 1, that data is the NSData data for a dynamic picture. If the current watch platform is developed by:

1         1 ; 2         scale = [Wkinterfacedevice currentdevice].screenscale;

Gets the screenscale of the current device, if it is IOS/TV platform development (including the Uikit Framework) by:

1         1 ; 2         scale = [UIScreen mainscreen].scale;

Gets the scale of the current screen and assigns it to a float variable that controls the size of the first frame image of the returned dynamic picture.

7. Use CGImageSource.h: Cgimagesourcecreateimageatindex (source, 0, NULL) to get the Cgimageref instance cgimage with index 0 o'clock in source.

1 /* Return the image at ' index ' in the image source ' isrc '.  The indexis2  * zero-based. The ' Options ' dictionary may used to request additional3*/45 Imageio_extern cgimageref __nullable cgimagesourcecreateimageatindex (cgimagesourceref __nonnull isrc, size_t index, Cfdictionaryref __nullable options)  imageio_available_starting (__mac_10_4, __IPHONE_4_0);

8. If it is Ios/tv/watch platform development, according to the above obtained cgimage and scale to do parameters, using:

1 + (UIImage *) Imagewithcgimage: (cgimageref) Cgimage scale: (cgfloat) scale orientation: (uiimageorientation) Orientation Ns_available_ios (4_0);

Create a UIImage instance frameimage.

9. Put Frameimage in a nsarray for parameters, use:

1 + (Nullable UIImage *) Animatedimagewithimages: (nsarray<uiimage *> *) images Duration: (nstimeinterval) Duration Ns_available_ios (5_0);

Create a images for a picture, duration a 0.0f animated picture and assign it to staticimage. If it is a Mac platform development, still cgimage as parameters, using:

1 [[UIImage alloc] Initwithcgimage:cgimage size:nszerosize];

Create a UIImage instance to assign to Staticimage.

10. Release Cgimage using CGImage.h's Cgimagerelease (cgimage).

1 /*  */23void  cgimagerelease (cgimageref cg_nullable image)4      cg_available_starting (__mac_10_0, __iphone_2_0);

11. Release source using CFBase.h's cfrelease (source).

1 Cf_export 2 void Cfrelease (Cftyperef CF);

12. Return to Staticimage.

END

Reference Link: http://www.jianshu.com/p/d3e9e3d0a778

Sdwebimage source Reading (c) uiimage+gif

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.