0

我正在尝试使用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 再次管理图像?

4

2 回答 2

3

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

于 2011-09-09T18:03:45.877 回答
0

如果你只有一个图像,你可以使用[objMan emptyCache]; 如果你只想刷新一个图像,你可以将它保存在不同的目录中,并使用这个方法来代替:

[objMan deleteAllFilesInDir:path];

当然,您将不得不再次进行查询:

[self.objMan manage:img1];
于 2011-09-09T10:58:17.563 回答