版本記錄
版本號(hào) | 時(shí)間 |
---|---|
V1.0 | 2018.04.01 |
前言
iOS圈內(nèi)有幾個(gè)人大家基本都知道碴犬,比如說(shuō)王巍、唐巧,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等停做。這里有一篇唐巧對(duì)他的專訪,還有他的 GitHub - Yaoyuan 和 博客大莫,這里貼出來(lái)框架YYKit 框架蛉腌。接下來(lái)幾篇我們就一起來(lái)看一下這個(gè)框架。感興趣的可以看上面寫的幾篇只厘。
1. YYKit源碼探究(一) —— 基本概覽
2. YYKit源碼探究(二) —— NSString分類之Hash(一)
3. YYKit源碼探究(三) —— NSString分類之Encode and decode(二)
4. YYKit源碼探究(四) —— NSString分類之Drawing(三)
5. YYKit源碼探究(五) —— NSString分類之Regular Expression(四)
6. YYKit源碼探究(六) —— NSString分類之NSNumber Compatible(五)
7. YYKit源碼探究(七) —— NSString分類之Utilities(六)
8. YYKit源碼探究(八) —— NSNumber分類(一)
9. YYKit源碼探究(九) —— UIFont分類之架構(gòu)分析和Font Traits(一)
10. YYKit源碼探究(十) —— UIFont分類之Create font(二)
11. YYKit源碼探究(十一) —— UIFont分類之Load and unload font(三)
12. YYKit源碼探究(十二) —— UIFont分類之Dump font data(四)
13. YYKit源碼探究(十三) —— UIImage分類之框架結(jié)構(gòu)和Create image部分(一)
14. YYKit源碼探究(十四) —— UIImage分類之Image Info(二)
15. YYKit源碼探究(十五) —— UIImage分類之Modify Image(三)
16. YYKit源碼探究(十六) —— UIImage分類之Image Effect(四)
17. YYKit源碼探究(十七) —— UIImageView分類之架構(gòu)和image部分(一)
18. YYKit源碼探究(十八) —— UIImageView分類之highlight image部分(二)
19. YYKit源碼探究(十九) —— UIScreen分類(一)
20. YYKit源碼探究(二十) —— UIScrollView分類(一)
21. YYKit源碼探究(二十一) —— UITableView分類(一)
22. YYKit源碼探究(二十二) —— UITextField分類(一)
23. YYKit源碼探究(二十三) —— UIView分類(一)
24. YYKit源碼探究(二十四) —— UIPasteboard分類(一)
25. YYKit源碼探究(二十五) —— UIGestureRecognizer分類(一)
26. YYKit源碼探究(二十六) —— UIDevice分類框架及Device Information(一)
27. YYKit源碼探究(二十七) —— UIDevice分類之Network Information(二)
28. YYKit源碼探究(二十八) —— UIDevice分類之Disk Space(三)
29. YYKit源碼探究(二十九) —— UIDevice分類之Memory Information(四)
30. YYKit源碼探究(三十) —— UIDevice分類之CPU Information(五)
31. YYKit源碼探究(三十一) —— UIControl分類(一)
32. YYKit源碼探究(三十二) —— UIColor分類之Create a UIColor Object(一)
33. YYKit源碼探究(三十三) —— UIColor分類之Get color's description(二)
34. YYKit源碼探究(三十四) —— UIColor分類之Retrieving Color Information(三)
35. YYKit源碼探究(三十五) —— UIButton分類之image(一)
36. YYKit源碼探究(三十六) —— UIButton分類之background image(二)
37. YYKit源碼探究(三十七) —— UIBezierPath分類(一)
38. YYKit源碼探究(三十八) —— UIBarButtonItem分類(一)
39. YYKit源碼探究(三十九) —— UIApplication分類(一)
40. YYKit源碼探究(四十) —— NSTimer分類(一)
41. YYKit源碼探究(四十一) —— NSParagraphStyle分類(一)
42. YYKit源碼探究(四十二) —— NSObject分類之YYModel(一)
43. YYKit源碼探究(四十三) —— NSObject分類之KVO(二)
44. YYKit源碼探究(四十四) —— NSObject分類之Sending messages with variable parameters(三)
45. YYKit源碼探究(四十五) —— NSObject分類之Swap method (Swizzling)(四)
46. YYKit源碼探究(四十六) —— NSObject分類之Associate value(五)
47. YYKit源碼探究(四十七) —— NSObject分類之Other(六)
48. YYKit源碼探究(四十八) —— NSNotificationCenter分類(一)
49. YYKit源碼探究(四十九) —— NSKeyedUnarchiver分類(一)
50. YYKit源碼探究(五十) —— NSDictionary分類之Dictionary Convertor(一)
51. YYKit源碼探究(五十一) —— NSDictionary分類之Dictionary Value Getter(二)
52. YYKit源碼探究(五十二) —— NSDictionary分類之NSMutableDictionary(三)
53. YYKit源碼探究(五十三) —— NSDate分類之Component Properties(一)
54. YYKit源碼探究(五十四) —— NSDate分類之Date modify(二)
55. YYKit源碼探究(五十五) —— NSDate分類之Date Format(三)
56. YYKit源碼探究(五十六) —— NSData分類之Hash(一)
57. YYKit源碼探究(五十七) —— NSData分類之Encrypt and Decrypt(二)
58. YYKit源碼探究(五十八) —— NSData分類之Encode and decode(三)
59. YYKit源碼探究(五十九) —— NSData分類之Inflate and deflate(四)
60. YYKit源碼探究(六十) —— NSData分類之Others(五)
61. YYKit源碼探究(六十一) —— NSBundle分類(一)
62. YYKit源碼探究(六十二) —— NSAttributedString分類之基本(一)
63. YYKit源碼探究(六十三) —— NSAttributedString分類之Retrieving character attribute information(二)
64. YYKit源碼探究(六十四) —— NSAttributedString分類之Get character attribute as property(三)
65. YYKit源碼探究(六十五) —— NSAttributedString分類之Get paragraph attribute as property(四)
66. YYKit源碼探究(六十六) —— NSAttributedString分類之Get YYText attribute as property(五)
67. YYKit源碼探究(六十七) —— NSAttributedString分類之Query for YYText(六)
68. YYKit源碼探究(六十八) —— NSAttributedString分類之Create attachment string for YYText(七)
69. YYKit源碼探究(六十九) —— NSAttributedString分類之Utility(八)
70. YYKit源碼探究(七十) —— NSMutableAttributedString分類之Set character attribute(九)
71. YYKit源碼探究(七十一) —— NSMutableAttributedString分類之Set character attribute as property(十)
72. YYKit源碼探究(七十二) —— NSMutableAttributedString分類之Set paragraph attribute as property(十一)
73. YYKit源碼探究(七十三) —— NSMutableAttributedString分類之Set YYText attribute as property(十二)
74. YYKit源碼探究(七十四) —— NSMutableAttributedString分類之Set discontinuous attribute for range(十三)
75. YYKit源碼探究(七十五) —— NSMutableAttributedString分類之Convenience methods for text highlight(十四)
76. YYKit源碼探究(七十六) —— NSMutableAttributedString分類之Utilities(十五)
77. YYKit源碼探究(七十七) —— NSArray分類(一)
78. YYKit源碼探究(七十八) —— NSMutableArray分類(一)
79. YYKit源碼探究(七十九) —— MKAnnotationView分類(一)
回顧
上一篇主要介紹了MKAnnotationView
分類烙丛,這一篇主要看一下CALayer
分類之YYWebImage
部分。
API
下面我們看一下API
/**
Current image URL.
@discussion Set a new value to this property will cancel the previous request
operation and create a new request operation to fetch image. Set nil to clear
the image and image URL.
*/
@property (nullable, nonatomic, strong) NSURL *imageURL;
/**
Set the view's `image` with a specified URL.
@param imageURL The image url (remote or local file path).
@param placeholder The image to be set initially, until the image request finishes.
*/
- (void)setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder;
/**
Set the view's `image` with a specified URL.
@param imageURL The image url (remote or local file path).
@param options The options to use when request the image.
*/
- (void)setImageWithURL:(nullable NSURL *)imageURL options:(YYWebImageOptions)options;
/**
Set the view's `image` with a specified URL.
@param imageURL The image url (remote or local file path).
@param placeholder The image to be set initially, until the image request finishes.
@param options The options to use when request the image.
@param completion The block invoked (on main thread) when image request completed.
*/
- (void)setImageWithURL:(nullable NSURL *)imageURL
placeholder:(nullable UIImage *)placeholder
options:(YYWebImageOptions)options
completion:(nullable YYWebImageCompletionBlock)completion;
/**
Set the view's `image` with a specified URL.
@param imageURL The image url (remote or local file path).
@param placeholder The image to be set initially, until the image request finishes.
@param options The options to use when request the image.
@param progress The block invoked (on main thread) during image request.
@param transform The block invoked (on background thread) to do additional image process.
@param completion The block invoked (on main thread) when image request completed.
*/
- (void)setImageWithURL:(nullable NSURL *)imageURL
placeholder:(nullable UIImage *)placeholder
options:(YYWebImageOptions)options
progress:(nullable YYWebImageProgressBlock)progress
transform:(nullable YYWebImageTransformBlock)transform
completion:(nullable YYWebImageCompletionBlock)completion;
/**
Set the view's `image` with a specified URL.
@param imageURL The image url (remote or local file path).
@param placeholder he image to be set initially, until the image request finishes.
@param options The options to use when request the image.
@param manager The manager to create image request operation.
@param progress The block invoked (on main thread) during image request.
@param transform The block invoked (on background thread) to do additional image process.
@param completion The block invoked (on main thread) when image request completed.
*/
- (void)setImageWithURL:(nullable NSURL *)imageURL
placeholder:(nullable UIImage *)placeholder
options:(YYWebImageOptions)options
manager:(nullable YYWebImageManager *)manager
progress:(nullable YYWebImageProgressBlock)progress
transform:(nullable YYWebImageTransformBlock)transform
completion:(nullable YYWebImageCompletionBlock)completion;
/**
Cancel the current image request.
*/
- (void)cancelCurrentImageRequest;
下面詳細(xì)的看一下羔味。
1. @property (nullable, nonatomic, strong) NSURL *imageURL;
為此屬性設(shè)置一個(gè)新值將取消先前的請(qǐng)求操作并創(chuàng)建一個(gè)新的請(qǐng)求操作來(lái)獲取圖像蜀变。 設(shè)置為nil以清除圖片和圖片URL。
方法實(shí)現(xiàn)
- (NSURL *)imageURL {
_YYWebImageSetter *setter = objc_getAssociatedObject(self, &_YYWebImageSetterKey);
return setter.imageURL;
}
- (void)setImageURL:(NSURL *)imageURL {
[self setImageWithURL:imageURL
placeholder:nil
options:kNilOptions
manager:nil
progress:nil
transform:nil
completion:nil];
}
2. - (void)setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder;
根據(jù)指定的URL設(shè)置view的圖像介评。
方法實(shí)現(xiàn)
- (void)setImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)placeholder {
[self setImageWithURL:imageURL
placeholder:placeholder
options:kNilOptions
manager:nil
progress:nil
transform:nil
completion:nil];
}
3. - (void)setImageWithURL:(nullable NSURL *)imageURL options:(YYWebImageOptions)options;
根據(jù)指定的URL設(shè)置view的圖像库北。
方法實(shí)現(xiàn)
- (void)setImageWithURL:(NSURL *)imageURL options:(YYWebImageOptions)options {
[self setImageWithURL:imageURL
placeholder:nil
options:options
manager:nil
progress:nil
transform:nil
completion:nil];
}
4. - (void)setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options completion:(nullable YYWebImageCompletionBlock)completion;
根據(jù)指定的URL設(shè)置view的圖像。
方法實(shí)現(xiàn)
- (void)setImageWithURL:(NSURL *)imageURL
placeholder:(UIImage *)placeholder
options:(YYWebImageOptions)options
completion:(YYWebImageCompletionBlock)completion {
[self setImageWithURL:imageURL
placeholder:placeholder
options:options
manager:nil
progress:nil
transform:nil
completion:completion];
}
5. - (void)setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion;
根據(jù)指定的URL設(shè)置view的圖像们陆。
方法實(shí)現(xiàn)
- (void)setImageWithURL:(NSURL *)imageURL
placeholder:(UIImage *)placeholder
options:(YYWebImageOptions)options
progress:(YYWebImageProgressBlock)progress
transform:(YYWebImageTransformBlock)transform
completion:(YYWebImageCompletionBlock)completion {
[self setImageWithURL:imageURL
placeholder:placeholder
options:options
manager:nil
progress:progress
transform:transform
completion:completion];
}
6. - (void)setImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder options:(YYWebImageOptions)options manager:(nullable YYWebImageManager *)manager progress:(nullable YYWebImageProgressBlock)progress transform:(nullable YYWebImageTransformBlock)transform completion:(nullable YYWebImageCompletionBlock)completion;
根據(jù)指定的URL設(shè)置view的圖像寒瓦。
方法實(shí)現(xiàn)
- (void)setImageWithURL:(NSURL *)imageURL
placeholder:(UIImage *)placeholder
options:(YYWebImageOptions)options
manager:(YYWebImageManager *)manager
progress:(YYWebImageProgressBlock)progress
transform:(YYWebImageTransformBlock)transform
completion:(YYWebImageCompletionBlock)completion {
if ([imageURL isKindOfClass:[NSString class]]) imageURL = [NSURL URLWithString:(id)imageURL];
manager = manager ? manager : [YYWebImageManager sharedManager];
_YYWebImageSetter *setter = objc_getAssociatedObject(self, &_YYWebImageSetterKey);
if (!setter) {
setter = [_YYWebImageSetter new];
objc_setAssociatedObject(self, &_YYWebImageSetterKey, setter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
int32_t sentinel = [setter cancelWithNewURL:imageURL];
dispatch_async_on_main_queue(^{
if ((options & YYWebImageOptionSetImageWithFadeAnimation) &&
!(options & YYWebImageOptionAvoidSetImage)) {
[self removeAnimationForKey:_YYWebImageFadeAnimationKey];
}
if (!imageURL) {
if (!(options & YYWebImageOptionIgnorePlaceHolder)) {
self.contents = (id)placeholder.CGImage;
}
return;
}
// get the image from memory as quickly as possible
UIImage *imageFromMemory = nil;
if (manager.cache &&
!(options & YYWebImageOptionUseNSURLCache) &&
!(options & YYWebImageOptionRefreshImageCache)) {
imageFromMemory = [manager.cache getImageForKey:[manager cacheKeyForURL:imageURL] withType:YYImageCacheTypeMemory];
}
if (imageFromMemory) {
if (!(options & YYWebImageOptionAvoidSetImage)) {
self.contents = (id)imageFromMemory.CGImage;
}
if(completion) completion(imageFromMemory, imageURL, YYWebImageFromMemoryCacheFast, YYWebImageStageFinished, nil);
return;
}
if (!(options & YYWebImageOptionIgnorePlaceHolder)) {
self.contents = (id)placeholder.CGImage;
}
__weak typeof(self) _self = self;
dispatch_async([_YYWebImageSetter setterQueue], ^{
YYWebImageProgressBlock _progress = nil;
if (progress) _progress = ^(NSInteger receivedSize, NSInteger expectedSize) {
dispatch_async(dispatch_get_main_queue(), ^{
progress(receivedSize, expectedSize);
});
};
__block int32_t newSentinel = 0;
__block __weak typeof(setter) weakSetter = nil;
YYWebImageCompletionBlock _completion = ^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) {
__strong typeof(_self) self = _self;
BOOL setImage = (stage == YYWebImageStageFinished || stage == YYWebImageStageProgress) && image && !(options & YYWebImageOptionAvoidSetImage);
BOOL showFade = (options & YYWebImageOptionSetImageWithFadeAnimation);
dispatch_async(dispatch_get_main_queue(), ^{
BOOL sentinelChanged = weakSetter && weakSetter.sentinel != newSentinel;
if (setImage && self && !sentinelChanged) {
if (showFade) {
CATransition *transition = [CATransition animation];
transition.duration = stage == YYWebImageStageFinished ? _YYWebImageFadeTime : _YYWebImageProgressiveFadeTime;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionFade;
[self addAnimation:transition forKey:_YYWebImageFadeAnimationKey];
}
self.contents = (id)image.CGImage;
}
if (completion) {
if (sentinelChanged) {
completion(nil, url, YYWebImageFromNone, YYWebImageStageCancelled, nil);
} else {
completion(image, url, from, stage, error);
}
}
});
};
newSentinel = [setter setOperationWithSentinel:sentinel url:imageURL options:options manager:manager progress:_progress transform:transform completion:_completion];
weakSetter = setter;
});
});
}
7. - (void)cancelCurrentImageRequest;
取消當(dāng)前圖像下載。
方法實(shí)現(xiàn)
- (void)cancelCurrentImageRequest {
_YYWebImageSetter *setter = objc_getAssociatedObject(self, &_YYWebImageSetterKey);
if (setter) [setter cancel];
}
后記
本篇文章主要介紹了CALayer分類的YYWebImage部分坪仇,感興趣的給個(gè)贊或者關(guān)注~~~