EGOImageLoader图片加载

来源:互联网 发布:mac电脑卡机怎么解决 编辑:程序博客网 时间:2024/06/03 10:24

[[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:observerselector:@selector(imageLoaderDidLoad:)name:kImageNotificationLoaded(aURL)object:self];

}


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

     [[NSNotificationCenterdefaultCenter]addObserver:observerselector:@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:aURLdelegate:self];

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

[connectionsLocklock];

[currentConnectionssetObject:connectionforKey: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:NSURLRequestReturnCacheDataElseLoadtimeoutInterval: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 = [UIImageimageWithData:connection.responseData];


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

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


[currentConnections removeObjectForKey:connection.imageURL];

self.currentConnections = [[currentConnectionscopy]autorelease];


#if __EGOIL_USE_NOTIF

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

object:self

  userInfo:[NSDictionarydictionaryWithObjectsAndKeys: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的委托方法就可以得到图片了。


0 0
原创粉丝点击