版本记录
版本号 | 时间 |
---|---|
V1.0 | 2018.02.10 |
前言
我们做APP,文字和图片是绝对不可缺少的元素,特别是图片一般存储在图床里面,一般公司可以委托第三方保存,NB的公司也可以自己存储图片,ios有很多图片加载的第三方框架,其中最优秀的莫过于SDWebImage,它几乎可以满足你所有的需求,用了好几年这个框架,今天想总结一下。感兴趣的可以看其他几篇。
1. SDWebImage探究(一)
2. SDWebImage探究(二)
3. SDWebImage探究(三)
4. SDWebImage探究(四)
5. SDWebImage探究(五)
6. SDWebImage探究(六) —— 图片类型判断深入研究
一般枚举
大家都用过枚举,一般的枚举,比如下面这种:
typedef NS_ENUM(NSInteger, UIViewContentMode) {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
};
这个填充模式的枚举,每次使用的时候我们都只能使用其中的一个元素,比如使用UIViewContentModeScaleToFill
,而且在定义枚举值的时候我们可以指定从一个整数值开始,后面的值依次比前一个值加1;如果不指定值,那么第一个成员的值默认为0,后面的值还是依次递增为1, 2......,这个使用起来很简单了,也不用多说。
位移枚举
这个与上面的一般枚举的区别就在于,使用的时候可以使用多个值,比如苹果原生的API中很多这种位移枚举:
typedef NS_OPTIONS(NSUInteger, UIViewAnimationOptions) {
UIViewAnimationOptionLayoutSubviews = 1 << 0,
UIViewAnimationOptionAllowUserInteraction = 1 << 1, // turn on user interaction while animating
UIViewAnimationOptionBeginFromCurrentState = 1 << 2, // start all views from current value, not initial value
UIViewAnimationOptionRepeat = 1 << 3, // repeat animation indefinitely
UIViewAnimationOptionAutoreverse = 1 << 4, // if repeat, run animation back and forth
UIViewAnimationOptionOverrideInheritedDuration = 1 << 5, // ignore nested duration
UIViewAnimationOptionOverrideInheritedCurve = 1 << 6, // ignore nested curve
UIViewAnimationOptionAllowAnimatedContent = 1 << 7, // animate contents (applies to transitions only)
UIViewAnimationOptionShowHideTransitionViews = 1 << 8, // flip to/from hidden state instead of adding/removing
UIViewAnimationOptionOverrideInheritedOptions = 1 << 9, // do not inherit any options or animation type
UIViewAnimationOptionCurveEaseInOut = 0 << 16, // default
UIViewAnimationOptionCurveEaseIn = 1 << 16,
UIViewAnimationOptionCurveEaseOut = 2 << 16,
UIViewAnimationOptionCurveLinear = 3 << 16,
UIViewAnimationOptionTransitionNone = 0 << 20, // default
UIViewAnimationOptionTransitionFlipFromLeft = 1 << 20,
UIViewAnimationOptionTransitionFlipFromRight = 2 << 20,
UIViewAnimationOptionTransitionCurlUp = 3 << 20,
UIViewAnimationOptionTransitionCurlDown = 4 << 20,
UIViewAnimationOptionTransitionCrossDissolve = 5 << 20,
UIViewAnimationOptionTransitionFlipFromTop = 6 << 20,
UIViewAnimationOptionTransitionFlipFromBottom = 7 << 20,
UIViewAnimationOptionPreferredFramesPerSecondDefault = 0 << 24,
UIViewAnimationOptionPreferredFramesPerSecond60 = 3 << 24,
UIViewAnimationOptionPreferredFramesPerSecond30 = 7 << 24,
} NS_ENUM_AVAILABLE_IOS(4_0);
这种枚举我们就可以一次选择多个值,比如说我们可以使用UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionAllowUserInteraction
,这么写我们就是使用该枚举的前两个成员。这样写的作用就是既可以布局子视图,还可以允许用户交互。
但是位移枚举的原理是怎样的呢?
其实给一个枚举变量赋予多个枚举值的时候,原理只是把各个枚举值加起来罢了。当加起来以后,就获取了一个新的值,那么为了保证这个值的唯一性,这个时候就体现了位运算的重要作用。位运算可以确保枚举值组合的唯一性。其实|
这个按位或的符号就是将多个枚举值加起来的意思。
下面基于SDWebImage一个options的按位枚举和大家说一下该枚举值的使用方法,先看一下代码。
#import "ViewController.h"
typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
SDWebImageRetryFailed = 1 << 0,
SDWebImageLowPriority = 1 << 1,
SDWebImageCacheMemoryOnly = 1 << 2,
SDWebImageProgressiveDownload = 1 << 3,
SDWebImageRefreshCached = 1 << 4,
SDWebImageContinueInBackground = 1 << 5,
SDWebImageHandleCookies = 1 << 6,
SDWebImageAllowInvalidSSLCertificates = 1 << 7,
SDWebImageHighPriority = 1 << 8,
SDWebImageDelayPlaceholder = 1 << 9,
SDWebImageTransformAnimatedImage = 1 << 10,
SDWebImageAvoidAutoSetImage = 1 << 11,
SDWebImageScaleDownLargeImages = 1 << 12
};
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"SDWebImageRetryFailed = %ld", SDWebImageRetryFailed);
NSLog(@"SDWebImageLowPriority = %ld", SDWebImageLowPriority);
NSLog(@"SDWebImageCacheMemoryOnly = %ld", SDWebImageCacheMemoryOnly);
NSLog(@"SDWebImageProgressiveDownload = %ld", SDWebImageProgressiveDownload);
NSLog(@"SDWebImageRefreshCached = %ld", SDWebImageRefreshCached);
NSLog(@"SDWebImageContinueInBackground = %ld", SDWebImageContinueInBackground);
NSLog(@"SDWebImageDelayPlaceholder = %ld", SDWebImageDelayPlaceholder);
NSLog(@"SDWebImageScaleDownLargeImages = %ld", SDWebImageScaleDownLargeImages);
SDWebImageOptions option1 = SDWebImageDelayPlaceholder;
SDWebImageOptions option2 = SDWebImageContinueInBackground;
NSLog(@"-------- & --------");
NSLog(@"%ld", option1 & option2);
NSLog(@"%ld", option1 & option1);
NSLog(@"-------- | --------");
NSLog(@"%ld", option1 | option2);
NSLog(@"%ld", option1 | option1);
}
@end
重要的就是看输出
2018-02-11 10:21:39.294440+0800 JJWebImage[4506:1119756] SDWebImageRetryFailed = 1
2018-02-11 10:21:39.294518+0800 JJWebImage[4506:1119756] SDWebImageLowPriority = 2
2018-02-11 10:21:39.294543+0800 JJWebImage[4506:1119756] SDWebImageCacheMemoryOnly = 4
2018-02-11 10:21:39.294564+0800 JJWebImage[4506:1119756] SDWebImageProgressiveDownload = 8
2018-02-11 10:21:39.294585+0800 JJWebImage[4506:1119756] SDWebImageRefreshCached = 16
2018-02-11 10:21:39.294657+0800 JJWebImage[4506:1119756] SDWebImageContinueInBackground = 32
2018-02-11 10:21:39.294684+0800 JJWebImage[4506:1119756] SDWebImageDelayPlaceholder = 512
2018-02-11 10:21:39.294707+0800 JJWebImage[4506:1119756] SDWebImageScaleDownLargeImages = 4096
2018-02-11 10:21:39.294728+0800 JJWebImage[4506:1119756] -------- & --------
2018-02-11 10:21:39.294773+0800 JJWebImage[4506:1119756] 0
2018-02-11 10:21:39.294796+0800 JJWebImage[4506:1119756] 512
2018-02-11 10:21:39.294815+0800 JJWebImage[4506:1119756] -------- | --------
2018-02-11 10:21:39.294833+0800 JJWebImage[4506:1119756] 544
2018-02-11 10:21:39.299656+0800 JJWebImage[4506:1119756] 512
这里可以看到:
- 两个不同的枚举值按位与
&
其实就是0,相同的按位&
就是其本身。 - 两个不同的枚举值按位或
|
其实就是两个枚举值的相加,相同的按位或|
就是其本身。
这个就是按位枚举的基本定义,希望能帮助到不怎么使用和探究原理的小伙伴。
后记
本篇已结束,后面更精彩~~~