iphone - 如何使用HJCache刷新图片?

标签 iphone objective-c ios hjcache

我正在尝试使用 HJCache 在我的应用程序中显示来自网络摄像头的图像.
网络摄像头每 5 分钟刷新一次图像。
我想要一个刷新按钮,以便用户可以单击它来查看新图像(如果可用)。
到目前为止我的代码:

-(void)viewDidLoad {
    // init HJObjManager
    objMan = [[HJObjManager alloc] initWithLoadingBufferSize:6 memCacheSize:20];
    // refresh button
    UIBarButtonItem *buttonRefresh = [[UIBarButtonItem alloc]
                     initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                     target:self
                     action:@selector(refreshPhoto:)];
    self.navigationItem.rightBarButtonItem = buttonRefresh;
    [buttonRefresh release];
    NSURL *url =  [NSURL URLWithString: @"http://webcamurl"];
    img1.url = url;
    [self.objMan manage:img1];
}

-(IBAction) refreshPhoto: (id) sender {
    // ?
}

你能给我一个关于如何实现 refreshPhoto 的提示吗?

编辑:安德将我指向 emptyCache。如果我没看错的话,应该是HJMOFileCache用的,所以我现在的代码是:

-(void)viewDidLoad {
    NSString *documentsDirectory;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    documentsDirectory = [paths objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"imageCache/"];
    objMan = [[HJObjManager alloc] initWithLoadingBufferSize:6 memCacheSize:20];
    HJMOFileCache* fileCache = [[[HJMOFileCache alloc] initWithRootPath:documentsDirectory] autorelease];
    fileCache.fileCountLimit = 100;
    fileCache.fileAgeLimit = 300; // 5 min
    objMan.fileCache = fileCache;
    // refresh button 
    UIBarButtonItem *buttonRefresh = [[UIBarButtonItem alloc]
                     initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
                     target:self
                     action:@selector(refreshPhoto:)];
    self.navigationItem.rightBarButtonItem = buttonRefresh;
    [buttonRefresh release];
    NSURL *url =  [NSURL URLWithString: @"http://webcamurl"];
    img1.url = url;
    [self.objMan manage:img1];
    [super viewDidLoad];
}

-(IBAction) refreshPhoto: (id) sender {
    [self.objMan.fileCache emptyCache];
    [self.objMan manage:img1];
}

虽然它不起作用,当我点击刷新按钮时没有任何反应,图像没有刷新。
有什么想法吗?

编辑: ender 建议缓存文件可能不会被 emptyCache 删除(如果我理解正确的话),但看起来它们确实会删除。
来自 emptyCache 前后的 NSLog:

2011-09-09 16:57:33.842 Ready dir before emptyCache: (
    "http:__www.meteogallipoli.it_cam_cam1.jpg"
)
2011-09-09 16:57:33.845 Loading dir before emptyCache: (
)
2011-09-09 16:57:33.856 Ready dir after emptyCache: (
)
2011-09-09 16:57:33.859 Loading dir after emptyCache: (
)

“Ready”和“Loading”分别是objMan存放已经下载和正在下载的文件的目录。
也许问题在于让 objMan 再次管理图像?

最佳答案

我认为这是因为您为对象管理器配置了文件缓存和内存缓存。当你清空文件缓存时,内存缓存中还有图像?尝试用

实例化对象管理器

关于iphone - 如何使用HJCache刷新图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7360488/

相关文章:

iphone - 重用多个 UIStoryboard 的 View

iOS 自动布局不在底部固定空间处显示按钮

iphone - 所选单元格的背景图像

iphone - 如何在iphone sdk上将NSData转换为CGDataProviderRef?

ios - 如何在顶部中心设置标签

iphone - NSPredicate 查询父子

objective-c - 带原始位图数据的高效 Cocoa 动画

ios - UIViewController 到 UIImagePickerController 到其他 UIViewController

ios - 在 Xcode 中,如何在没有付费 Apple Developer 帐户的情况下注册设备进行测试?

iphone - 将 subview 添加到 AppDelegate