我正在尝试使用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的提示吗?
编辑: ender 将我指向 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 再次管理图像?