带有图像缓存和“拉动更新"的表视图选项 [英] Table view with image caching and "pull to update" option

查看:19
本文介绍了带有图像缓存和“拉动更新"的表视图选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在几乎每个 ios 应用都有类似Feed"的选项.编程通常包括从网络获取图像、缓存它们、处理页面、拉取更新选项"等 - 所有标准的东西.

Almost every ios app now has something like "Feed" option. Programming that usually includes fetching images from the web, caching them, handling pages, "pull to update option", etc. - all the standart stuff.

但是看起来没有标准的解决方案吗?

But looks like there is no standart solution for that?

  • 我尝试了三个 20"——非常大、复杂的库,有很多模块.它真的缺乏好的文档!它在从缓存中获取图像时也出现了减速".

  • I tried "three 20" - really big, complicated library with many modules. It really lacks good documentation! And it also had "slowdowns" while fetching images from cache.

也许我应该为每个小任务分别使用不同的小库?如HJCache、EGO等

Maybe I should use different small libraries for each small task separately? Like HJCache, EGO, etc.

还是从头开始编写所有内容而不使用任何库更好?

Or is it better to write everything from scratch without any libraries?

请就这里的最佳实践给我建议,我现在真的很困惑.

Please give me advice on best practices here, i am really stuck now.

推荐答案

这个很容易上手下拉刷新.

对于图片加载,我为 UIImageView 编写了以下类别:

For image loading, I wrote the following category for UIImageView:

// .h
@interface UIImageView (UIImageView_Load)
- (void)loadFrom:(NSURL *)url completion:(void (^)(UIImage *))completion;
@end

// .m
#import "UIImageView+Load.h"
#import <QuartzCore/QuartzCore.h>

@implementation UIImageView (UIImageView_Load)

- (void)loadFrom:(NSURL *)url completion:(void (^)(UIImage *))completion {

    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (data) {
            self.image = [UIImage imageWithData:data];
            if (completion) completion(self.image);
        }
    }];
}

@end

// To use it when building a cell

//...

MyModelObject *myModelObject = [self.myModel objectAtIndex:indexPath.row];
if (myModelObject.image) {
    cell.imageView.image = myModelObject.image;
} else {
    NSURL *url = [NSURL urlWithString:myModelObject.imageUrl];
    [cell.imageView loadFrom:url completion:^(UIImage *image) {
        // cache it in the model
        myModelObject.image = image;
        cell.imageView.image = image;
    }];
}

// ...

这篇关于带有图像缓存和“拉动更新"的表视图选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆