版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2018.03.25 |
前言
iOS圈內(nèi)有幾個人大家基本都知道朵栖,比如說王巍、唐巧剔宪,還有YYKit框架的作者現(xiàn)任職于滴滴的郭曜源 - ibireme等拂铡。這里有一篇唐巧對他的專訪,還有他的 GitHub - Yaoyuan 和 博客葱绒,這里貼出來框架YYKit 框架感帅。接下來幾篇我們就一起來看一下這個框架。感興趣的可以看上面寫的幾篇地淀。
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部分(一)
回顧
上一篇主要介紹了UIImageView
分類之架構(gòu)和image部分失球,這一篇主要看一下highlight image
部分。
API
下面我們看一下API帮毁。
#pragma mark - highlight image
/**
Current highlighted 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 highlighted image and image URL.
*/
@property (nullable, nonatomic, strong) NSURL *highlightedImageURL;
/**
Set the view's `highlightedImage` 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)setHighlightedImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder;
/**
Set the view's `highlightedImage` 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)setHighlightedImageWithURL:(nullable NSURL *)imageURL options:(YYWebImageOptions)options;
/**
Set the view's `highlightedImage` 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)setHighlightedImageWithURL:(nullable NSURL *)imageURL
placeholder:(nullable UIImage *)placeholder
options:(YYWebImageOptions)options
completion:(nullable YYWebImageCompletionBlock)completion;
/**
Set the view's `highlightedImage` 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)setHighlightedImageWithURL:(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 `highlightedImage` 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)setHighlightedImageWithURL:(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 highlighed image request.
*/
- (void)cancelCurrentHighlightedImageRequest;
這個API文檔和上一篇都是類似的实苞,這里不同的是這個是高亮狀態(tài),那么只給出第一個和最后一個方法的實現(xiàn)吧烈疚。
1. - (void)setHighlightedImageWithURL:(nullable NSURL *)imageURL placeholder:(nullable UIImage *)placeholder;
該方法的作用就是根據(jù)給定的URL設(shè)置下載的圖片黔牵。
方法實現(xiàn)
- (void)setHighlightedImageWithURL:(NSURL *)imageURL placeholder:(UIImage *)placeholder {
[self setHighlightedImageWithURL:imageURL
placeholder:placeholder
options:kNilOptions
manager:nil
progress:nil
transform:nil
completion:nil];
}
- (void)setHighlightedImageWithURL:(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, &_YYWebImageHighlightedSetterKey);
if (!setter) {
setter = [_YYWebImageSetter new];
objc_setAssociatedObject(self, &_YYWebImageHighlightedSetterKey, setter, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
int32_t sentinel = [setter cancelWithNewURL:imageURL];
dispatch_async_on_main_queue(^{
if ((options & YYWebImageOptionSetImageWithFadeAnimation) &&
!(options & YYWebImageOptionAvoidSetImage)) {
if (self.highlighted) {
[self.layer removeAnimationForKey:_YYWebImageFadeAnimationKey];
}
}
if (!imageURL) {
if (!(options & YYWebImageOptionIgnorePlaceHolder)) {
self.highlightedImage = placeholder;
}
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.highlightedImage = imageFromMemory;
}
if(completion) completion(imageFromMemory, imageURL, YYWebImageFromMemoryCacheFast, YYWebImageStageFinished, nil);
return;
}
if (!(options & YYWebImageOptionIgnorePlaceHolder)) {
self.highlightedImage = placeholder;
}
__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) && self.highlighted);
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.layer addAnimation:transition forKey:_YYWebImageFadeAnimationKey];
}
self.highlightedImage = image;
}
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;
});
});
}
2. - (void)cancelCurrentHighlightedImageRequest;
該方法的作用取消下載高亮圖片。
方法實現(xiàn)
- (void)cancelCurrentHighlightedImageRequest {
_YYWebImageSetter *setter = objc_getAssociatedObject(self, &_YYWebImageHighlightedSetterKey);
if (setter) [setter cancel];
}
- (int32_t)cancel {
return [self cancelWithNewURL:nil];
}
- (int32_t)cancelWithNewURL:(NSURL *)imageURL {
int32_t sentinel;
dispatch_semaphore_wait(_lock, DISPATCH_TIME_FOREVER);
if (_operation) {
[_operation cancel];
_operation = nil;
}
_imageURL = imageURL;
sentinel = OSAtomicIncrement32(&_sentinel);
dispatch_semaphore_signal(_lock);
return sentinel;
}
后記
本篇主要講述了UIImageView的
highlight image
部分爷肝,感興趣的可以給個贊或者關(guān)注猾浦,謝謝~~~