EGOImageView 使用方法
EGOImageView是一种实现网络图片的异步加载和缓存的第三方类库
加载一个网络图片 :
EGOImageView *imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]]; imageView.imageURL = [NSURL URLWithString:imageURL];imageView.frame = CGRectMake(x,x,x,x);
placeholder.png是加载图片未完成时显示的图片,当加载过程完成之后就会显示url对应的图片。
清理缓存:
[[EGOCache currentCache] clearCache];
获得 下载好的图片 :
UIImage *getImage = [[EGOImageLoader sharedImageLoader] imageForURL:[NSURLURLWithString:@"http://imgsrc.baidu.com/forum/pic/item/9d82d158ccbf6c811c42177ebc3eb13533fa402a.jpg"]shouldLoadWithObserver:nil ];
需要特别注意的是EGOImageView在多线程下的使用
当imageView的图片加载完成了,这时你想换一个图片的url并用EGOImageView加载这个图片时,需要重新设置EGOImageView的imageURL属性。但是这里要特别注意的是这个方法必须在主线程中执行
[imageView performSelectorOnMainThread:@selector(setImageURL:) withObject:[NSURL URLWithString:@"http://....../images/2.jpg"] waitUntilDone:NO];
注:多线程编程中普遍遵循一个原则,就是一切与UI相关的操作都有主线程做,子线程只负责事务,数据方面的处理。那么如果想在子线程中更新UI时怎么做呢?在iPhone中,需要使用performSelectorOnMainThread 委托主线程处理。