我们如何在NSMutableArray中添加带有Image作为对象的NSMutableDictionary,并在Plist中编写此数组

时间:2014-04-03 11:51:22

标签: ios uiimage nsmutablearray plist nsmutabledictionary

我想将UIImage存储在NSMutableDictionary中。

NSData *imgData = UIImagePNGRepresentation(picture.image);
[studDict setValue:imgData forKey:@"Photo"];

其中, studDict 是NSMutableDictionary。 和图片是UIImageView。

并将此dictionnary add作为对象存储在NSMutableArray中。

    [studArrayPlist addObject:detailDict];

其中, studArrayPlist 是NSMutableArray。 detailDict 是带有UIImage的NSMutableDictionary。

并且这个NSMutableArray用plist写。

[studArrayPlist writeToFile:plistPath atomically: TRUE];

其中, studArrayPlist 是NSMutableArray存储带有UIImage的NSMutableDictionary。

这不能用plist写。 并且不要给出任何错误。 我还想从plist中检索数组中的所有数据。

2 个答案:

答案 0 :(得分:3)

检查对象是否已添加到词典& array.If然后不分配NSMutableArray&的NSMutableDictionary。

NSMutableDictionary *studDict=[[NSMutableDictionary alloc] init];

[studDict setValue:imgData forKey:@"Photo"];

NSMutableArray * studArrayPlist =[[NSMutableArray alloc] init];
[studArrayPlist addObject:studDict];
[studArrayPlist writeToFile: plistPath atomically:YES];

答案 1 :(得分:0)

是的我正在这样做,它运作正常但有时候工作缓慢。

现在, 用于将图像存储为数据

NSMutableDictionary *studDict=[[NSMutableDictionary alloc] init];

NSData *imgData = UIImagePNGRepresentation(picture.image);
[studDict setValue:imgData forKey:@"Photo"];

NSMutableArray * studArrayPlist =[[NSMutableArray alloc] init];
[studArrayPlist addObject:studDict];
[studArrayPlist writeToFile: plistPath atomically:YES];

用于将图像作为数据检索,

NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);

// get documents path
NSString *documentsPath = [paths objectAtIndex:0];

// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"StudDetail.plist"];

//Storing all the records (NSDictionary objects) in array from plist.
studeInfo = [[NSMutableArray alloc]initWithContentsOfFile:plistPath];


UIImage *img = [[UIImage alloc] initWithData:[[studeInfo objectAtIndex:"your index"] objectForKey:@"Photo"]];
[person setImage:img];
[img release];