Guest User

Untitled

a guest
Jan 22nd, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. @implementation Contacts
  2.  
  3. + (UIImage *)imageForContactWithName:(NSString *)name {
  4.  
  5. // Create a CF string reference from the NSString pointer
  6. CFStringRef cfName = objc_unretainedPointer(name);
  7. UIImage *personImage = nil;
  8.  
  9. // Create a reference to the address book
  10. ABAddressBookRef addressBook = ABAddressBookCreate();
  11.  
  12. // Create a reference to an array of all the people that match the passed name string
  13. CFArrayRef people = ABAddressBookCopyPeopleWithName(addressBook, cfName);
  14. int num = CFArrayGetCount(people);
  15.  
  16. // Create a reference to the first match if there was a match
  17. ABRecordRef person = (num > 0) ? CFArrayGetValueAtIndex(people, 0) : NULL;
  18.  
  19. // If there was a matching contact and that contact had image data
  20. if (num > 0 && ABPersonHasImageData(person)) {
  21.  
  22. // Have to use objc_unretainedObject for toll-free bridging in Xcode 4.3 w/ ARC
  23. CFDataRef imageData = ABPersonCopyImageData(person);
  24. NSData *personImageData = [NSData dataWithData:(NSData *)objc_unretainedObject(imageData)];
  25. CFRelease(imageData);
  26.  
  27. personImage = [UIImage imageWithData:personImageData];
  28. } else if (num > 0) {
  29. // Contact existed but there was no image data
  30. personImage = [UIImage imageNamed:@"smile.png"];
  31. } else {
  32. // Contact did not exist
  33. personImage = [UIImage imageNamed:@"frown.png"];
  34. }
  35.  
  36. CFRelease(addressBook);
  37. CFRelease(people);
  38.  
  39. return personImage;
  40. }
  41.  
  42. @end
Add Comment
Please, Sign In to add comment