博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
EGOImageView 解析
阅读量:6988 次
发布时间:2019-06-27

本文共 2527 字,大约阅读时间需要 8 分钟。

hot3.png

在看EGOImageLoader源码的时候首先个人感觉要先理解里面的几个概念

1.inline 开头的函数表示内联函数。作用是用函数名直接代替表达式,也就是说执行到这一行代码的时候不会去调用函数,而是直接执行函数体。同样也有一定限制,那就是函数体不能太复杂,不能有循环和开关语句。最适合就是取值,而在EGOImageLoader这个框架中就是根据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的作用是返回一个全局唯一的数字

2.需要了解的是EGO用的是观察者模式来回调函数,也就是说在调用imageForURL方法的时候会根据URL创建属于这个图片的专属通知,当加载完成后会发出通知,image会在notification的userinfo里面存着,并以object的形式伴随通知发过去。

****************************************************************

使用EGOImageView,

1.创建对象

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];

        //引用计数减一并置空

        [imageURLrelease];

        imageURL = nil;

     }

     if(!aURL) {

        //设置默认图片

        self.image = self.placeholderImage;

        imageURL = nil;

       return;

     } else {

        //引用计数加一并且赋给imageURL

        imageURL = [aURL retain];

     }

     [[EGOImageLoadersharedImageLoader] removeObserver:self];

         //调用loader的方法来根据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

你可能感兴趣的文章
JQuery 获取验证码倒计时
查看>>
scala(13)-----集合(Collection)-------元组
查看>>
线段树模板
查看>>
tomcat下发布项目,遇到的问题总结
查看>>
Node开发入门
查看>>
持续集成①安装部署jenkins从git获取代码
查看>>
zabbix添加对haproxy的监控
查看>>
WP开发笔记——程序的退出方法
查看>>
StreamingAssets文件夹的读取异常
查看>>
【文文殿下】【BZOJ4804】欧拉心算
查看>>
python mat
查看>>
Docker技术入门与实战 第二版-学习笔记-8-网络功能network-2-相应配置
查看>>
收集网址
查看>>
python文本操作
查看>>
本周学习小结(01/04 - 07/04)
查看>>
精选CSDN的ACM-ICPC博文
查看>>
C语言的面向对象技术(转)
查看>>
C/C++读写二进制文件
查看>>
经典SQL语句(转载)
查看>>
#从零开始学SWIFT2.0# NO.5 可选变量和集合
查看>>