ios - 从 NSMutable Dictionary 获取一个对象并将其设置为新键。

标签 ios xcode nsmutabledictionary

我有一个称为缓存图像的方法。这使用 NSNotification 中心来发布图像已被缓存的信息。我有数据 NSMutableDictionary *userInfo。我正在尝试添加一个观察者来检索图像并使用 _URL 作为键进行保存。问题是 URL 和图像都作为具有自己的键的对象添加到字典中。是否可以检索相应 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;
    }

 }

在我的另一个类 viewcontroller.m 文件中

   -(void)ViewDidLoad{

     [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(imageLoaded:)
                                             name:AsyncImageLoadDidFinish
                                           object:nil];
    }


     - (void)imageLoaded:(NSNotification *)notification
  {

    NSMutableDictionary *imageCache = notification.object;// help needed here
   }

我正在尝试添加一个观察者,但我不知道如何使用对象“_URL”作为图像的键。使用观察者接收对象后,我必须找到该 URL 的图像缓存。

最佳答案

您现在拥有此代码:

NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                     image, AsyncImageImageKey,
                                     _URL, AsyncImageURLKey,
                                     nil];

所以当你想将其添加到imageCache时:

NSURL *_url = [userInfo objectForKey:AsyncImageURLKey];
UIImage *image = [userInfo objectForKey:AsyncImageImageKey];

[imageCache setObject:image forKey:_url];

关于ios - 从 NSMutable Dictionary 获取一个对象并将其设置为新键。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11933515/

相关文章:

ios - 我无法让我的球 SKSpriteNode 显示在辅助 View Controller 的 UIImage(Xcode/Swift 2) 上

ios - 是否有 NSMapTable 的 NSMutableArray 版本?

ios - AVSynchronizedLayer 动画适用于本地文件但在流式传输时效果不佳

ios - 具有多个参数和 "AND behaviour"的 NSPredicate

ios - 带有 TableView 和手势识别器问题的 Swift/UIView

ios - Xcode 泄漏工具卡在 MapkKit/CllocationManager Ios8 上

ios - 如何正确设置同时具有框架目标和使用该框架的应用程序目标的 Podfile

xcode - Mercurial 和 xcuserdata、.ds_store 和 .git

ios - NSMutableDictionary 返回 null

ios - 在不使用 for 循环的情况下更新字典数组中的键值对