牛骨文教育服务平台(让学习变的简单)

@EGOImageView是一个很常用的异步加载及缓存网络图片的第三方类,相比SDWebImage,个人感觉EGOImageView更简单(PS:EGOImageView不支持ARC,SDWebImage3.0支持ARC)

@EGOImageView的导入
1.下载:https://github.com/enormego/EGOImageLoading(下载后运行demo程序XCode会提示找不到EGOCache.h头文件,可以在这个地方下载https://github.com/enormego/EGOCache)
2.将EGOCache、EGOImageButton、EGOImageView、EGOImageLoader全部添加到工程下(拷贝)
3.EGOImageView是不支持ARC的,在ARC的工程中要注意,可参考MRC工程配置ARC

@代码示例:

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    _button = [[HMTBlockButton alloc]initWithFrame:CGRectMake(110, 510, 100, 50)];
    _button.backgroundColor = [UIColor redColor];
    [_button setTitle:@"加载" forState:UIControlStateNormal];
    [self.view addSubview:_button];
    [_button release];
    
    // 将系统给的Button封装成了Block形式
    _button.blockButton = ^(HMTBlockButton * button){
        /**
         *  placeholder.png 是图片还未加载完成时显示的图片,在APP中经常能看到图片未加载完成显示是该APP主题的
         *  图片,当加载过程完成之后就会显示url对应的图片。
         */
        _imageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];
        _imageView.imageURL = [NSURL URLWithString:@"http://i0.sinaimg.cn/ent/s/m/2011-08-19/U3904P28T3D3391507F329DT20110819143720.jpg"];
        _imageView.frame = CGRectMake(60, 30, 200, 400);
        [self.view addSubview:_imageView];
        [_imageView release];
        
    };
    
    // 作用:清除缓存
    [[EGOCache globalCache] clearCache];
    
    // 获得已经下载了的图片对象
    UIImage * image = [[EGOImageLoader sharedImageLoader]imageForURL:[NSURL URLWithString:@"http://i0.sinaimg.cn/ent/s/m/2011-08-19/U3904P28T3D3391507F329DT20110819143720.jpg"] shouldLoadWithObserver:nil];
    
}

@注意事项:

         最近上网查资料看到,用EGOImageView的一个bug

当imageView的图片加载完成了,这时你想换一个图片的url并用EGOImageView加载这个图片时,需要重新设置EGOImageView的imageURL属性。但是这里要特别注意的是这个方法必须在主线程中执行

    _button1 = [[HMTBlockButton alloc]initWithFrame:CGRectMake(160, 510, 100, 50)];
    _button1.backgroundColor = [UIColor redColor];
    [_button1 setTitle:@"换图" forState:UIControlStateNormal];
    [self.view addSubview:_button1];
    [_button1 release];
    
    // 将系统给的Button封装成了Block形式
    _button1.blockButton = ^(HMTBlockButton * button){
        
        // 在主线程中进行
        dispatch_async(dispatch_get_main_queue(), ^{
            
            _imageView.imageURL = [NSURL URLWithString:@"http://www.sinaimg.cn/dy/slidenews/3_img/2012_10/28891_147237_598867.jpg"];
            
        });
        
    };