EGOImageView 解析

在看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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值