简体   繁体   中英

TableView Cell image persistence using Core Data

I am trying an app with a tableview which has profile name and images of people..the tableview is editable and adding new name and picture, deleting an existing one and rearranging the table are possible..the tableview and changes to it should be saved using core data..I have almost completed it but core data part is having some problems..

I have implemented core data to provide data persistence for the tablecells..everytime i restart the app, the table cell keeps the changes that were last made..but the cell image doesn't load..how to make that image persist?

NSManagedObject *person1 = [NSEntityDescription
                            insertNewObjectForEntityForName:@"Person"
                            inManagedObjectContext:context];
NSManagedObject *person2 = [NSEntityDescription
                            insertNewObjectForEntityForName:@"Person"
                            inManagedObjectContext:context];

[person1 setValue:@"Bruce" forKey:@"personName"];
UIImage *image1 = [UIImage imageNamed:@"image1.jpg"]; 
NSData *imageData1 = UIImageJPEGRepresentation(image1, 0.9);
[person1 setValue:imageData1 forKey:@"personPicture"];
NSLog(@"Image1 data %@",imageData1);

[person2 setValue:@"Alfred" forKey:@"personName"];
UIImage *image2 = [UIImage imageNamed:@"image2.jpg"]; 
NSData *imageData2 = UIImageJPEGRepresentation(image2, 0.9);
[person2 setValue:imageData2 forKey:@"personPicture"];

NSError *error;
if (![context save:&error]) {
        NSLog(@"Whoops, couldn't save: %@", [error localizedDescription]);
}

then fetch was done as

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
                               entityForName:@"Person" inManagedObjectContext:context];

[fetchRequest setEntity:entity];
NSError *error1 = nil;
self.persons = [[context executeFetchRequest:fetchRequest error:&error1] mutableCopy];

this much was done in didFinishLaunchingWithOptions in appDelegate. The data was then used in the cellForRowAtIndexPath method in the tableview class implementation file as

Person *thisPerson = [persons objectAtIndex:indexPath.row];
cell.textLabel.text = thisPerson.personName;

UIImage *image = [UIImage imageWithData:[thisPerson valueForKey:@"personPicture"]];
if ( image == nil ) {
    image = [UIImage imageNamed:@"QuestionMark.jpg"]; 
}
cell.imageView.image = image;

The cell is loading properly..and all the editing part I did is also being saved and reloaded..the images load properly when loading the app for the 1st time..when it is restarted the QuestionMark.jpg image appears..which is because the image is nil..I tried NSLog to display the 1st image data and got this:

2012-09-07 17:05:59.395 Sample Project[1688:fb03] Image1 data <ffd8ffe0 00104a46     
49460001 01000001 00010000 ffe10058 45786966 00004d4d 002a0000 00080002 01120003 
00000001 00010000 87690004 00000001 00000026 00000000 0003a001 00030000 00010001 
0000a002 00040000 00010000 00bea003 00040000 00010000 01090000 .........
......               .............. 7ff4d668 0356803f 357e307f c94ff14f fd858ffe 
9af4ba00 f39a00cf a00d0a00 cfa00280 0a002803 ffd9>

a lot more was there where i added the dots..anyway..this seems to show that its saving the data..

If the image is relatively small, then save it as a NSData object in the core data repository. I do this for thumbnails etc. For huge images, you can save a URL and put the image in your Documents folder, and save a URL to the file (NSURL) as an object in Core Data too. If you delete that core data entity, it has to then delete the file.

EDIT: Regarding the "Sorry..." comment in your Question, change this line:

UIImage *image = [UIImage imageWithData:[thisPerson valueForKey:@"personPicture"]];

to

NSData *data = thisPerson.personPicture;
if(data) {
  NSLog(@"Image class: %@", NSStringFromClass[data class]);
  if([data isKindOfClass:[NSData data]]) {
    NSLog(@"Data was of size %u", [data length]);
    UIImage *image = [UIImage imageWithData:data];
    if(image) NSLOg(@"SUCCESS!");
    else NSLog(@"PROBLEM: cannot create image from data");
  }
} else {
  NSLog(@"BIG PROBEM - personPicture is nil");
}

You will learn much from the output here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM