EGOImageView 解析
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
在看EGOImageLoader源碼的時候首先個人感覺要先理解里面的幾個概念
1.inline 開頭的函數(shù)表示內聯(lián)函數(shù)。作用是用函數(shù)名直接代替表達式,也就是說執(zhí)行到這一行代碼的時候不會去調用函數(shù),而是直接執(zhí)行函數(shù)體。同樣也有一定限制,那就是函數(shù)體不能太復雜,不能有循環(huán)和開關語句。最適合就是取值,而在EGOImageLoader這個框架中就是根據(jù)URL生成一個key返回。
inline?static?NSString* keyForURL(NSURL* url, NSString* style) {
?????if(style) {
???????????return [NSStringstringWithFormat:@"EGOImageLoader-%u-%u", [[url description] hash], [style hash]];
?????} else {
????? ?????return [NSStringstringWithFormat:@"EGOImageLoader-%u", [[url description] hash]];
?????}
}
********hash的作用是返回一個全局唯一的數(shù)字
2.需要了解的是EGO用的是觀察者模式來回調函數(shù),也就是說在調用imageForURL方法的時候會根據(jù)URL創(chuàng)建屬于這個圖片的專屬通知,當加載完成后會發(fā)出通知,image會在notification的userinfo里面存著,并以object的形式伴隨通知發(fā)過去。
****************************************************************
使用EGOImageView,
1.創(chuàng)建對象
imageView = [[EGOImageViewalloc] initWithPlaceholderImage:[UIImageimageNamed:@"placeholder.png"]];
imageView.frame = CGRectMake(4.0f, 4.0f, 36.0f, 36.0f);
[self.contentViewaddSubview:imageView];
2.把URL賦給imageURL這個屬性
imageView.imageURL = [NSURLURLWithString:flickrPhoto];
3.在賦給這個屬性的時候會自動調用屬性的set方法,- (void)setImageURL:(NSURL *)aURL ;
在這個方法里,用注釋的形式寫出來
? ? ?if(imageURL) {
??????? //注銷兩個通知
????? ? [[EGOImageLoadersharedImageLoader] removeObserver:selfforURL:imageURL];
??????? //引用計數(shù)減一并置空
????? ??[imageURLrelease];
????? ? imageURL = nil;
?????}
?????if(!aURL) {
??????? //設置默認圖片
????? ? self.image = self.placeholderImage;
???? ? ?imageURL = nil;
??? ???return;
?????} else {
??????? //引用計數(shù)加一并且賦給imageURL
???? ? ?imageURL = [aURL retain];
?????}
?????[[EGOImageLoadersharedImageLoader] removeObserver:self];
? ? ? ? ?//調用loader的方法來根據(jù)URL加載圖片
? ? ?UIImage* anImage = [[EGOImageLoader?sharedImageLoader] imageForURL:aURL shouldLoadWithObserver:self];
? ? ?//如果緩存有側會立刻返回,如果緩存沒有則會先設置默認圖片,當加載圖片完畢的時候會以通知的方法進行回調
?????if(anImage) {
??????????self.image = anImage;
??????????// trigger the delegate callback if the image was found in the cache
??????????if([self.delegaterespondsToSelector:@selector(imageViewLoadedImage:)]) {
??????????[self.delegateimageViewLoadedImage:self];
??????????}
?????} else {
??????????self.image = self.placeholderImage;
?????}
4.通知回調imageLoaderDidLoad這個方法
- (void)imageLoaderDidLoad:(NSNotification*)notification {
if(![[[notification userInfo] objectForKey:@"imageURL"] isEqual:self.imageURL]) return;
?????UIImage* anImage = [[notification userInfo] objectForKey:@"image"];
?????self.image = anImage;
??????????//這個方法會調用drawrect的方法
??????????[selfsetNeedsDisplay];
?????if([self.delegaterespondsToSelector:@selector(imageViewLoadedImage:)]) {
??????????[self.delegateimageViewLoadedImage:self];
?????}
}
5.在這個類最重要的方法就是?[[EGOImageLoader?sharedImageLoader]?imageForURL:aURL?shouldLoadWithObserver:self];
這個方法了,第一個是傳URL,第二個是傳觀察者的對象。通過這個方法來加載圖片,成功后會調用委托方法- (void)imageLoaderDidLoad:(NSNotification*)notification ;
轉載于:https://my.oschina.net/sunqichao/blog/94506
總結
以上是生活随笔為你收集整理的EGOImageView 解析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS 6.3安装Nginx开启目
- 下一篇: 企业信息管理- 近期功能改善(3)