This oneは、プルしてリフレッシュするのは非常に簡単です。画像の読み込みについては
は、私は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;
}];
}
// ...
答えてくれてありがとう、@ダン!残念ながら、キャッシングソリューションは含まれていません。しかし、私はレアの「プッシュする」という試みを試みます。 – Danik
@ダニク - 確かに。私が貼り付けたそのコードは、コールバックのモデル内で遅延ロードとキャッシュを行います。 – danh
はい、今ご覧のとおり、myModelObject.imageにキャッシュします。 didReceiveMemoryWarningでキャッシュをクリアするか、最大サイズを制限しますか? – Danik