title: Access contact image from address book based on name
body: <p>Err,I have been pulling my hair thinking about a way from quite a few days.I have retrieved all contacts names and placed in an array using dictionary.</p>
  
  <p>What I have is a model class holding a list of names,now I want to search the location of name in contacts list,depending on which I can retrieve the required contact image.</p>
  
  <p>Initially googled and found out an unanswered question not pretty much similar to my requirement,the same can be glanced <a href="https://stackoverflow.com/questions/11574545/access-contact-image-from-address-book-based-on-its-id">here</a></p>
  
  <p>I tried several ways,the below is one way I have implemented:</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  {
      [self loadReminders];
  
      ReminderClass *reminderToDisplay = [self.remindersArray objectAtIndex:indexPath.row];
  
      UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellIdentifier];
  
      // Now create the cell to display the reminder data
      if (cell == nil) 
      {
          cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kCellIdentifier] autorelease];
          cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
          cell.textLabel.numberOfLines = 0;
          cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:17.0];
          cell.textLabel.adjustsFontSizeToFitWidth = YES;
      }
  
      tableView.backgroundColor = [UIColor clearColor];
  
      NSDateFormatter *dateFormat = [[[NSDateFormatter alloc]init]autorelease];
      [dateFormat setDateFormat:kDateFormat];
      NSDate *reminderDate = [dateFormat dateFromString:reminderToDisplay.Date]; 
      [dateFormat setDateFormat:kMinDateFormat]; 
      NSString *dateString = [dateFormat stringFromDate:reminderDate];
  
      NSString *valueString = [NSString stringWithFormat:@"%@'s %@",reminderToDisplay.Name,reminderToDisplay.Event];
      NSString *onString = [NSString stringWithFormat:@" on %@",dateString];
      NSString *reminderDetailsString = [valueString stringByAppendingString:onString];
  
      //Get the contact image based on name index from contact list
      ABAddressBookRef addressBook = ABAddressBookCreate( );
      CFStringRef reminderName = (CFStringRef)reminderToDisplay.Name;
      CFArrayRef allPeople = ABAddressBookCopyPeopleWithName(addressBook, reminderName);
      self.contactsList =[[[NSMutableArray alloc]init]autorelease];
  
      CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
      for ( int i = 0; i &lt; nPeople; i++ ) 
      { 
      ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i); 
      NSString *contactFirstNamePart = (NSString *)ABRecordCopyValue(ref,kABPersonFirstNameProperty);
      NSString *contactFirstName = [[[NSString alloc] initWithString:contactFirstNamePart]autorelease];
      NSString *contactLastNamePart = (NSString *)ABRecordCopyValue(ref, kABPersonLastNameProperty);
  
      if (contactLastNamePart == nil)
      {
          self.contactName = contactFirstName;
      }
  
      else
      {
          NSString *contactLastName = [[[NSString alloc] initWithString:contactLastNamePart]autorelease];
          NSString *contactLastNameString = [NSString stringWithFormat:@" %@",contactLastName]; 
          self.contactName = [contactFirstName stringByAppendingString:contactLastNameString]; 
          CFRelease(contactLastNamePart);
      }
  
      NSDictionary *contactsDictionary = [NSDictionary dictionaryWithObjectsAndKeys:self.contactName, kContactName, [NSNumber numberWithInt:i], kContactIndex, nil];
      [self.contactsList addObject:contactsDictionary];
      CFRelease(contactFirstNamePart);
  }
  
  NSDictionary *contactsDictionary = [self.contactsList objectAtIndex:indexPath.row]; 
  self.contactName = [contactsDictionary objectForKey:kContactName]; 
  int addressIndex = [[contactsDictionary objectForKey:kContactIndex]integerValue];
  
  ABRecordRef recordReference = CFArrayGetValueAtIndex(allPeople, addressIndex);
  if (ABPersonHasImageData(recordReference))
  {
      NSData *imageData = (NSData *)ABPersonCopyImageData(recordReference);
      self.reminderImage = [UIImage imageWithData:imageData];
      CFRelease(imageData);
  }
  
  CFRelease(allPeople);
  CFRelease(addressBook);
  
      UIImage *notificationImage = reminderImage;
  
      if (notificationImage != nil) 
      {
          UIImageView *imageView=[[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)]autorelease];
          imageView.backgroundColor=[UIColor clearColor];
          [imageView setImage:notificationImage];
          cell.accessoryView = imageView;
      }
      else
      {
          UIImageView *imageView=[[[UIImageView alloc] initWithFrame:CGRectMake(240, 3, 70, 63)]autorelease];
          imageView.backgroundColor=[UIColor clearColor];
          UIImage *defaultImage = [UIImage imageNamed:kDefaultImage];
          [imageView setImage:defaultImage];
          cell.accessoryView = imageView;
      }
      cell.textLabel.text = reminderDetailsString;
      cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
      return cell;
  }
  </code></pre>
  
  <p><strong>Bad Access Error Screen shot</strong></p>
  
  <p><img src="https://i.stack.imgur.com/tD4Wx.png" alt="enter image description here"></p>
  
  <p>But I was unable to accomplish the required task.Can any one please guide me.</p>
  
  <p>Thanks all in advance :)</p>