Sdwebimagecompat is a sdwebimage configuration file that is compatible with all Apple platforms using conditional Compilation. From the source code can see Sdwebimage support the current Mac/ios/tv/watch platform, this adaptation of the compatibility of each platform, the development of the framework is Significant.
1. #import <TargetConditionals.h>
Import this header file to access the configuration options provided by the System. All of this is a macro definition that defines the various Apple system platforms and the various CPU types associated with the Macros. Mainly used for development of the time for different development environment to do configuration Use.
2. Conditional Compilation __objc_gc__
#ifdef __objc_gc__ #error Sdwebimage does not objective-c garbage Collection#endif
Objective-c a garbage collection mechanism that supports memory (garbage collection Abbreviation: GC). In MAC development is supported, but using Mrc/arc in IOS development is not supported for GC . After IOS 5, It started to support arc, helping developers better manage memory, simplify memory management, and improve development efficiency. Sdwebimage does not support gc, if the macro defines a __objc_gc__, it means that it is in the development environment that supports gc, directly error (#error). When the GC is started, all retain, autorelease, release, and Dealloc methods are ignored by the System.
3.sd_mac
#if ! Target_os_iphone &&! Target_os_ios &&! TARGET_OS_TV &&! Target_os_watch #define sd_mac 1#else #define sd_mac 0#endif
Determine if the current platform is not a Mac. TARGET_OS_MAC is defined in all platforms, such as mac, iPhone, Watch, tv, etc., So it is not feasible to use TARGET_OS_MAC to determine whether the Mac platform is Current. But there is one drawback to the way it is judged: when Apple comes up with a new platform, the criteria are changed .
4.sd_uikit
#if Target_os_ios | | TARGET_OS_TV #define sd_uikit 1#else #define sd_uikit 0#endif
IOS and TvOS are very similar, uikit exist in all two platforms, but watchOS is limited when using uikit. So defining Sd_uikit as the true condition is iOS and tvOS two platforms.
5.sd_ios&&sd_tv&&sd_watch
#if Target_os_ios #define sd_ios 1#else #define sd_ios 0#endif#if TARGET_OS_TV #define sd_tv 1#else #define sd_tv 0#endif#if target_os_watch #define sd_watch 1#else #define sd_watch 0# endif
These three macro definitions are used to differentiate between ios, TV, watch three Platforms.
6. Platform compatible adaptation
#ifSd_mac#import<AppKit/AppKit.h>#ifndef UIImage#defineUIImage Nsimage#endif#ifndef Uiimageview#defineUiimageview Nsimageview#endif#ifndef UIView#defineUIView NSView#endif#else //Sdwebimage does not support iOS versions below 5.0 #if__iphone_os_version_min_required! = 20000 && __iphone_os_version_min_required < __iphone_5_0#errorSdwebimage doesn ' t support Deployment Target version < 5.0#endif #ifSd_uikit#import<UIKit/UIKit.h>#endif #ifSd_watch#import<WatchKit/WatchKit.h>#endif#endif
- If Sd_mac is true, it is developed on the MacOS platform, introducing and defining three macro uiimage/uiimageview/uiview;
- Sdwebimage does not support versions below iOS 5.0;
- Introducing <UIKit/UIKit.h>; into the real time of Sd_uikit
- <WatchKit/WatchKit.h> is introduced when Sd_watch is True.
7. Ns_enum/ns_options
#ifndef Ns_enum #define Ns_enum (_type, _name) ENUM _name: _type _name; Enum _name: _type#endif#ifndef ns_options#define ns_options (_type, _name) enum _name: _type _name; Enum _name: _type#endif
Used for enumeration Types.
8.os_object_use_objc
#if OS_OBJECT_USE_OBJC #undef Sddispatchqueuerelease #undef sddispatchqueuesettersementics #define sddispatchqueuerelease (q) #define Sddispatchqueuesettersementics Strong #else # undef sddispatchqueuerelease #undef Sddispatchqueuesettersementics Span style= "color: #0000ff" > #define sddispatchqueuerelease (q) (dispatch_release (q)) #define sddispatchqueuesettersementics assign #endif
The OS_OBJECT_USE_OBJC macro definition appears only after the 6.0 Release. The macro definition is primarily for GCD, where objects in GCD are not involved in arc before 6.0, and after 6.0 are used under Arc GCD do not care about releasing the Issue. For the minimum SDK version >= IOS 6.0来 said that GCD objects have been included in the ARC management scope, we do not need to manually call dispatch_release, otherwise, at the time of SDK < 6.0, even if arc is turned on, this macro Os_obje CT_USE_OBJC is also not, that is to say, GCD objects must also be managed by Themselves.
If you develop a project with a minimum target of less than IOS 6.0 or Mac OS X 10.8, You should manage GCD objects yourself, and ARC does not manage them (dispatch_retain, dispatch_release).
If the minimum target for a project is IOS 6.0 or Mac OS X 10.8 or higher, ARC is already able to manage GCD objects, and GCD objects are like normal OC objects and should not use dispatch_retain, Ordispatch_rel Ease.
9.API
// given a picture, adjust the scale property with the parameter key to return the image below the corresponding resolution extern UIImage *sdscaledimageforkey (nsstring *key, UIImage *void(^sdwebimagenoparamsblock) (); extern NSString *const sdwebimageerrordomain; Static 5;
10.dispatch_main_async_safe
#ifndef Dispatch_main_async_safe #define Dispatch_main_async_safe (block) if0) { block (); Else { dispatch_async (dispatch_get_main_queue (), block); } #endif
Execute the task block safely in the main thread.
In the project, if the current thread is already the main course, it is possible that crash will appear in the call to Dispatch_async (dispatch_get_main_queue (), block). therefore, a judgment is made: the current thread is the main path, call block directly, if not, then call Dispatch_async (dispatch_get_main_queue (), block).
11. Global Method Sdscaledimageforkey
/** Given a picture, adjust the scale property with the parameter key, return the image below the corresponding resolution @param key picture name @param image resource Picture @return processing later picture*/inline UIImage*sdscaledimageforkey (nsstring * _nullable key, UIImage *_nullable Image) { //Exception Handling if(!Image) { returnnil; } #ifSd_macreturnimage;#elifSd_uikit | | Sd_watch//If it is a dynamic picture, such as a GIF image, the iteration is processed if((image.images). Count >0) {nsmutablearray<uiimage *> *scaledimages =[nsmutablearray array]; //iterate over each picture for(UIImage *tempimageinchimage.images) {[scaledimages addobject:sdscaledimageforkey (key, tempimage)]; } //synthesize a picture of the finished image with a motion picture return[UIImage animatedimagewithimages:scaledimages duration:image.duration]; } Else {#ifSd_watchif([[[wkinterfacedevice currentdevice] respondstoselector: @selector (screenscale)]) {#elifSd_uikitif([[[uiscreen mainscreen] respondstoselector: @selector (scale)]) {#endifCGFloat scale=1; //the length of "@2x.png" is 7, so this judgment is added here, very ingenious if(key.length >=8) {nsrange Range= [key Rangeofstring:@"@2x."]; if(range.location! =Nsnotfound) { scale=2.0; } Range= [key Rangeofstring:@"@3x."]; if(range.location! =Nsnotfound) { scale=3.0; } } //returns the image below the corresponding resolutionUIImage *scaledimage =[[UIImage alloc] Initwithcgimage:image. Cgimage Scale:scale orientation:image.imageOrientation]; Image=scaledimage; } returnimage; }#endif}
The Sdwebimagecompat of Sdwebimage