我正在尝试用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删除(如果我理解的话),但是看起来它们确实删除了。
来自NSLog之前和之后的emptyCache:
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: (
)“准备”和“加载”分别是objMan存储已经下载和正在下载的文件的目录。
也许问题是让objMan再次管理图像?
发布于 2011-09-09 18:03:45
我认为这是因为您将对象管理器配置为文件缓存和内存缓存。当您清空文件缓存时,内存缓存中还有图像吗?尝试用
发布于 2011-09-09 10:58:17
如果您只有一个图像,如果您只想刷新一个图像,可以使用[objMan emptyCache];,您可以将它保存在一个不同的dir中,然后使用以下方法:
[objMan deleteAllFilesInDir:path];当然,您必须再次进行查询:
[self.objMan manage:img1];https://stackoverflow.com/questions/7360488
复制相似问题