Question

I want to store UIImage in NSMutableDictionary.

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

where, studDict is NSMutableDictionary. and picture is UIImageView.

and store this dictionnary add as object in NSMutableArray.

    [studArrayPlist addObject:detailDict];

where, studArrayPlist is NSMutableArray. and detailDict is NSMutableDictionary with UIImage.

and this NSMutableArray write in plist.

[studArrayPlist writeToFile:plistPath atomically: TRUE];

where, studArrayPlist is NSMutableArray store NSMutableDictionary with UIImage.

This will not work not write in plist. and dont give any error. Also i want to retrive all the data in array from plist.

Was it helpful?

Solution

Check whether the object is added to the dictionary & array.If not then allocate 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];

OTHER TIPS

Yes i'm doing this and it woks properly but work slowly sometimes.

Now, for storing image as data

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

and for retriving image as data,

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];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top