0

I'm trying to display an image from a webcam in my app using HJCache.
The webcam refreshes its image every 5 minutes.
I'd like to have a refresh button, so that users could click it to see a new image (if available).
My code so far:

-(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 {
    // ?
}

Could you give me an hint on how to implement refreshPhoto?

Edit: ender pointed me to emptyCache. If I understand it ok, it should be used by HJMOFileCache, so my code now is:

-(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];
}

It doesn't work though, when I click the refresh button nothing happens, the image does not refresh.
Any idea?

Edit: ender suggested that maybe the cache files do not get deleted by emptyCache (if I understand it right), but it looks like they actually do.
From NSLog before and after the 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: (
)

"Ready" and "Loading" are the directories where objMan stores files already downloaded and being downloaded, respectively.
Maybe the problem is in making objMan manage the image again?

Yannick Blondeau
  • 9,465
  • 8
  • 52
  • 74
Antonio Giungato
  • 145
  • 1
  • 2
  • 10

2 Answers2

3

I think its because you configured the object manager with both a file cache and a memory cache. When you empty the file cache, there are still images in the memory cache? Try instanciating the object manager with

Mark Johnson
  • 1,105
  • 9
  • 8
  • ---Mark answer is not complete, he was so kind to reply to an email I sent him, full answer is: "I think its because you configured the object manager with both a file cache and a memory cache. When you empty the file cache, there are still images in the memory cache? Try instanciating the object manager with 0 size for the memory cache. Or, when you want to empty the cache, re-instanciate the object manager itself. I should probably have a emptyCache method on the object manager itself." And yes, instantiating with 0 memory size fixed my problem. Thanks Mark! – Antonio Giungato Sep 09 '11 at 20:09
0

if you only have an image, you can use [objMan emptyCache]; If you want to refresh only an image, you can save it in a different dir and use this method instead:

[objMan deleteAllFilesInDir:path];

Of course, you will have to make the query again:

[self.objMan manage:img1];
Youssef
  • 3,582
  • 1
  • 21
  • 28
  • Have you tried calling deleteAllFilesInDir? Just specify the same directory as before. – Youssef Sep 09 '11 at 14:20
  • Try calling [img1 clear] before calling [self.objMan manage:img1] – Youssef Sep 09 '11 at 15:14
  • Can't get this to work, sigh! I've uploaded a sample project [here](http://www.mediafire.com/?kkig54tts4s650c) if anyone want to tell me why I'm doing it wrong... thanks @ender – Antonio Giungato Sep 09 '11 at 16:13
  • In the end I managed to fix it. As you do not need cache, what I did was take a class that a coworker wrote and hacked it a bit, eliminating a couple of checks and always loading the image from the network. Now the code looks much cleaner ;) http://www.mediafire.com/?i682evhd1mj281p – Youssef Sep 09 '11 at 16:50
  • I'm speechless... thank you so much! I have to buy you a beer next time I'm in Barcelona! And your nickname is from the best book ever! ¡Que viva España! :) – Antonio Giungato Sep 09 '11 at 17:17
  • I take the word! By the way, the project name is very very funny – Youssef Sep 09 '11 at 20:05