我想保存到NSMutableDictionary

时间:2015-09-27 17:02:13

标签: ios uiimage save nsdictionary nsmutabledictionary

大家好!我有一个需要保存多个图像的应用程序。我想将这些图像保存在NSMutableDictionary中,因为它们可能会在其他时间被访问..

我有一个NSDictionaryFile类,看起来像这样设置为

@interface NSDictionaryFile : NSMutableDictionary

-(NSMutableDictionary *) mutDict {
    mutDict = [[NSMutableDictionary alloc]initWithCapacity:24];
    return mutDict;
}

-(void)  addToDictionary : (NSData *) nsData : (NSString *) key {
    NSString *tempStringKey = [NSString stringWithFormat:@"%@",key];
    [mutDict setObject:[NSData dataWithData:nsData] forKey:tempStringKey];
    NSLog(@"The Key is %@ And The nsData has this %@",key,nsData);
}

-(NSData *) getFromDict : (NSData *) getNSData : (NSString *) getKey {
    NSString *tempStringKey = [NSString stringWithFormat:@"%@",getKey];
    NSData *tempData = [mutDict objectForKey:tempStringKey];
    getNSData = [NSData dataWithData:tempData];
    return  getNSData; 
}

我将从

保存到上述课程
@interface PhotoViewController :    UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>


-(void) imagePickerController:(UIImagePickerController *)picker   didFinishPickingMediaWithInfo:(NSDictionary *)info {
    image = [info valueForKey:UIImagePickerControllerOriginalImage];
    NSData *imgData = UIImagePNGRepresentation(image);
    NSString *imageString;
    num = 1;
    imageString = [NSString stringWithFormat:@"imageKey%i",num];
    [[NSDictionaryFile sharedDictionary] addToDictionary:imgData :imageString];

我正在View Did Load中像这样检索它,

-(void)viewDidLoad {
    NSData *tempData;
    NSData *imageData;
    _num = 1;
    imageString = [NSString stringWithFormat:@"imageKey%i",_num];
    [[NSDictionaryFile sharedDictionary]getFromDict:tempData           :imageString];
    self.imageView.image = [UIImage imageWithData:imageData];
}

当数据保存到NSDictionaryFie.m时我知道它正在发生,因为我是NSLog(@"The Image Data is %@",nsData);而且我的打印输出正如预期的那样。

  

89504e47 0d0a1a0a 0000000D 49484452 00000215 00000155 08020000 00d7368a d8000000 01735247 4200aece 1ce90000 001c6944 4f540000 00020000 00000000 00ab0000 00280000 00ab0000 00aa0003 41798433 c89e0000 40004944 41547801 a4bd079c 2547752f 7c677636 2a2192f1 b3fd6c63 3f6cb009 42486857 4802990c 1212b2c0 809f6d6c 30185bc0 27139456 bbda202d ca5aadb4 2badc2ae 36e79d0d b3333b39 e77c676e cef9debe b7730ef5 fed53d33 5a09f8de 07dffdd5 afe77475 d5a953a7

所以,当PickerdidFinisPicking时,图像显示在我的imageView中,但是当我尝试检索它时,我得到null。 如果有人能告诉我哪里出错了,我们非常感谢。 问候 JZ

1 个答案:

答案 0 :(得分:1)

我认为错误非常简单: 您没有使用getFromDict的返回值,而是使用未初始化的变量imageData

我认为你应该调用你的方法并使用返回值,就像这样

-(void)viewDidLoad {
    NSData *tempData;
    NSData *imageData;
    _num = 1;
    imageString = [NSString stringWithFormat:@"imageKey%i",_num];
    imageData=[[NSDictionaryFile sharedDictionary]getFromDict:tempData           :imageString];
    self.imageView.image = [UIImage imageWithData:imageData];
}

除了getter方法对我来说似乎过于复杂:

-(NSData *) getFromDict : (NSData *) getNSData : (NSString *) getKey {
    NSString *tempStringKey = [NSString stringWithFormat:@"%@",getKey];
    NSData *tempData = [mutDict objectForKey:tempStringKey];
    getNSData = [NSData dataWithData:tempData];
    return  getNSData; 
}

您传递的方法是一个NSData,您只是用它来存储一个值并将其传递给调用者。它&#39;无用传递这样的参数。

你尝试过一种更苗条的方法吗?

-(NSData *) getFromDict : (NSString *) getKey { 
    return[mutDict objectForKey:getKey]; 
}