Reputation: 141
I have a method called cache image. This uses a NSNotification centre to post the info that image has been cached. I have the data NSMutableDictionary *userInfo. I'm trying to add an observer to retrieve the image and save using the _URL as the key. The problem is both URL and image are added to the dictionary as object with their own keys. Is it possible to retrieve the image for the corresponding key?
- (void)cacheImage:(UIImage *)image
{
if (!_cancelled)
{
if (image && _URL)
{
BOOL storeInCache = YES;
if ([_URL isFileURL])
{
if ([[[_URL absoluteURL] path] hasPrefix:[[NSBundle mainBundle] resourcePath]])
{
//do not store in cache
storeInCache = NO;
}
}
if (storeInCache)
{
[_cache setObject:image forKey:_URL];
}
}
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
image, AsyncImageImageKey,
_URL, AsyncImageURLKey,
nil];
NSLog(@"%@",_URL);
if (_cache)
{
[userInfo setObject:_cache forKey:AsyncImageCacheKey];
}
_loading = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:AsyncImageLoadDidFinish
object:_target
userInfo:[[userInfo copy] autorelease]];
}
else
{
_loading = NO;
_cancelled = NO;
}
}
in my other class viewcontroller.m file
-(void)ViewDidLoad{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(imageLoaded:)
name:AsyncImageLoadDidFinish
object:nil];
}
- (void)imageLoaded:(NSNotification *)notification
{
NSMutableDictionary *imageCache = notification.object;// help needed here
}
Im trying to add an observer , but I can't figure how to use the object '_URL' as a key for the image.After using the observer to receive the object, I have to find the imagecache for that URL.
Upvotes: 0
Views: 360
Reputation: 41622
You have this code now:
NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
image, AsyncImageImageKey,
_URL, AsyncImageURLKey,
nil];
So when you want to add it to the imageCache:
NSURL *_url = [userInfo objectForKey:AsyncImageURLKey];
UIImage *image = [userInfo objectForKey:AsyncImageImageKey];
[imageCache setObject:image forKey:_url];
Upvotes: 1