EGOImageLoader解析

[[EGOImageLoader sharedImageLoader] imageForURL:aURL shouldLoadWithObserver:self]

1.在这个方法中首先会从缓存中查找,如果已经存在则直接返回 

UIImage* anImage = [[EGOCachecurrentCache] imageForKey:keyForURL(aURL,nil)];



2.如果不存在进入到加载URL的方法中去

[self loadImageForURL:aURL observer:observer];

在这个方法中会首先创建两个观察者,一个是成功的方法,一个是失败的方法

if([observer respondsToSelector:@selector(imageLoaderDidLoad:)]) {

     [[NSNotificationCenterdefaultCenter] addObserver:observer selector:@selector(imageLoaderDidLoad:) name:kImageNotificationLoaded(aURL) object:self];

}


if([observer respondsToSelector:@selector(imageLoaderDidFailToLoad:)]) {

     [[NSNotificationCenterdefaultCenter] addObserver:observer selector:@selector(imageLoaderDidFailToLoad:) name:kImageNotificationLoadFailed(aURL) object:self];

}

之后开始加载URL

[self loadImageForURL:aURL];



3.在- (EGOImageLoadConnection*)loadImageForURL:(NSURL*)aURL;这个方法里面会创建EGOImageLoadConnection的对象,当然要首先检查一下这个connection = [selfloadingConnectionForURL:aURL]是否存在,如果存在就从currentConnections这个字典里面取出来

如果不存则需要重新创建(注意在存的时候需要加锁)

 onnection = [[EGOImageLoadConnection alloc] initWithImageURL:aURL delegate:self];

        //在存入字典的时候需要加锁,防止同一个connect重复存储的情况

[connectionsLocklock];

[currentConnectionssetObject:connection forKey:aURL];

self.currentConnections = [[currentConnectionscopy] autorelease];

[connectionsLockunlock];

[connection performSelector:@selector(start) withObject:nilafterDelay:0.01];

[connection release];


return connection;



4.而在start方法里面会根据URL创建对应的connection

NSMutableURLRequest* request = [[NSMutableURLRequestalloc] initWithURL:self.imageURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:self.timeoutInterval];

[request setValue:@"gzip"forHTTPHeaderField:@"Accept-Encoding"];  

_connection = [[NSURLConnection alloc] initWithRequest:request delegate:selfstartImmediately:YES];

[request release];

之后会调用NSURLConnection的委托方法来接受数据,当全部接受完成后回调imageLoadConnectionDidFinishLoading这个方法,并把自己传过去



5.在EGOImageLoader这里面实现imageLoadConnectionDidFinishLoading方法,把data转化为图片

UIImage* anImage = [UIImage imageWithData:connection.responseData];


并且把data存入到缓存中去,删除字典中存的那个connection,并发出通知,而图片就存在notification的userinfo里面

[[EGOCache currentCache] setData:connection.responseDataforKey:keyForURL(connection.imageURL,nil) withTimeoutInterval:604800];


[currentConnections removeObjectForKey:connection.imageURL];

self.currentConnections = [[currentConnections copy] autorelease];


#if __EGOIL_USE_NOTIF

NSNotification* notification = [NSNotification notificationWithName:kImageNotificationLoaded(connection.imageURL)

object:self

  userInfo:[NSDictionary dictionaryWithObjectsAndKeys:anImage,@"image",connection.imageURL,@"imageURL",nil]];

 

[[NSNotificationCenter defaultCenter] performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:YES];

#endif


#if __EGOIL_USE_BLOCKS

[self handleCompletionsForConnection:connection image:anImage error:nil];

#endif




6.在需要图片的类中实现imageLoaderDidLoad的委托方法就可以得到图片了。









转载于:https://my.oschina.net/sunqichao/blog/94508

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值