Note that there are some explanatory texts on larger screens.

plurals
  1. POFetching image from iOS CoreData
    text
    copied!<p>I have a Core Data entity defined with an <code>NSData</code> property <code>imageData</code>. I have a setter and getter setup for <code>image</code> on the entity.</p> <p>The setter, <code>setImage</code>, just calls <code>[self setImageData:UIImagePNGRepresentation(img)];</code> The getter, <code>image</code>, just returns <code>[UIImage imageWithData:[self imageData]];</code></p> <p>When I create a new entity and my fetched results controller loads the new table row, <strong>the image always pulls null</strong>. However, <strong>if I tap the row to go to the next view controller, the image shows up just fine</strong>.</p> <p>What can I do to make it show the image immediately after the save occurs and the fetchResultsController updates with the new row.</p> <p>EDIT: Adding code</p> <p>Core data header and getter/setter for image snippet</p> <pre><code>.h @property (nonatomic, retain) NSData * imageData; @property (nonatomic, retain) UIImage *image; .m - (void)setImage:(UIImage *)image { NSData *data = UIImagePNGRepresentation(image); [self setImageData:data]; } - (UIImage *)image { return [UIImage imageWithData:[self imageData]]; } </code></pre> <p>Table view code</p> <pre><code>- (NSFetchedResultsController *)fetchedResultsController { if (_fetchedResultsController != nil) { return _fetchedResultsController; } [NSFetchedResultsController deleteCacheWithName:@"Artists"]; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"ArtistModel" inManagedObjectContext:[[BKDataManager sharedInstance] mainObjectContext]]; [fetchRequest setEntity:entity]; NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; [fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]]; [fetchRequest setFetchBatchSize:20]; _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[[BKDataManager sharedInstance] mainObjectContext] sectionNameKeyPath:nil cacheName:@"Artists"]; _fetchedResultsController.delegate = self; return _fetchedResultsController; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { ArtistTableViewCell *cell = (ArtistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"ArtistsTableViewCell"]; if(!cell) { cell = (ArtistTableViewCell *)[[[UINib nibWithNibName:@"ArtistTableViewCell" bundle:nil] instantiateWithOwner:nil options:nil] objectAtIndex:0]; } ArtistModel *row = [[self fetchedResultsController] objectAtIndexPath:indexPath]; [[cell artistNameLabel] setText:[row name]]; [[cell artistImageView] setImage:[row image]]; // This is where it pulls from the artist model return cell; } </code></pre> <p>Saving to core data bit</p> <pre><code>- (void)addArtistWithName:(NSString *)name confirmed:(BOOL)confirmed managedContext:(NSManagedObjectContext *)managedContext success:(BKClientIdResult)success failure:(BKClientError)failure { NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys: name, @"name", [NSNumber numberWithBool:confirmed], @"confirmed" , nil]; [[BKClient sharedInstance] postPath:@"/users/artists" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) { if([[responseObject objectForKey:@"status"] boolValue]) { NSDictionary *result = [responseObject valueForKeyPath:@"payload.artist"]; ArtistModel *artistModel = (ArtistModel *)[NSEntityDescription insertNewObjectForEntityForName:@"ArtistModel" inManagedObjectContext:managedContext]; [artistModel setName:[result objectForKey:@"name"]]; NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[result objectForKey:@"image"]]]; AFImageRequestOperation *op = [AFImageRequestOperation imageRequestOperationWithRequest:imageRequest imageProcessingBlock:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { [artistModel setImage:image]; [managedContext save:nil]; if(success) { success(artistModel); } } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { // TODO FIX }]; [op start]; } else { if(failure) { failure([NSError errorWithDomain:@"com.nf" code:0 userInfo:[responseObject objectForKey:@"error"]]); } } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { if(failure) { failure(error); } }]; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload