From 9c9596edb28387101af1e05a345c80c59918b7b1 Mon Sep 17 00:00:00 2001 From: Alexey Belkevich Date: Mon, 20 Jan 2014 19:46:10 +0200 Subject: [PATCH] Refactored thumnail loading Updated readme --- .../AddressBook/ContactTableViewCell.m | 2 +- Classes/APContact.h | 2 +- Classes/APContact.m | 17 ++++++++---- Classes/APTypes.h | 27 ++++++++++--------- README.md | 7 +++++ 5 files changed, 35 insertions(+), 20 deletions(-) diff --git a/AddressBook/AddressBook/ContactTableViewCell.m b/AddressBook/AddressBook/ContactTableViewCell.m index 9158574..cd149b2 100644 --- a/AddressBook/AddressBook/ContactTableViewCell.m +++ b/AddressBook/AddressBook/ContactTableViewCell.m @@ -40,7 +40,7 @@ - (void)updateWithModel:(id)model self.companyLabel.text = contact.company ?: @"(No company)"; self.phonesLabel.text = [self contactPhones:contact]; self.emailsLabel.text = [self contactEmails:contact]; - self.photoView.image = contact.photo ?: [UIImage imageNamed:@"no_photo"]; + self.photoView.image = contact.thumbnail ?: [UIImage imageNamed:@"no_photo"]; } #pragma mark - private diff --git a/Classes/APContact.h b/Classes/APContact.h index 0876e74..abbd075 100644 --- a/Classes/APContact.h +++ b/Classes/APContact.h @@ -19,7 +19,7 @@ @property (nonatomic, readonly) NSArray *phones; @property (nonatomic, readonly) NSArray *emails; @property (nonatomic, readonly) UIImage *photo; -@property (nonatomic, readonly) UIImage *photoThumb; +@property (nonatomic, readonly) UIImage *thumbnail; - (id)initWithRecordRef:(ABRecordRef)recordRef fieldMask:(APContactField)fieldMask; diff --git a/Classes/APContact.m b/Classes/APContact.m index a2c9aa8..c4ee30f 100644 --- a/Classes/APContact.m +++ b/Classes/APContact.m @@ -40,13 +40,11 @@ - (id)initWithRecordRef:(ABRecordRef)recordRef fieldMask:(APContactField)fieldMa } if (fieldMask & APContactFieldPhoto) { - NSData *imageData = (__bridge_transfer NSData *)ABPersonCopyImageData(recordRef); - _photo = [UIImage imageWithData:imageData scale:UIScreen.mainScreen.scale]; + _photo = [self imagePropertyFullSize:YES fromRecord:recordRef]; } - if (fieldMask & APContactFieldPhotoThumb) + if (fieldMask & APContactFieldThumbnail) { - NSData *imageData = (__bridge_transfer NSData *)ABPersonCopyImageDataWithFormat(recordRef, kABPersonImageFormatThumbnail); - _photoThumb = [UIImage imageWithData:imageData scale:UIScreen.mainScreen.scale]; + _thumbnail = [self imagePropertyFullSize:NO fromRecord:recordRef]; } } return self; @@ -77,4 +75,13 @@ - (NSArray *)arrayProperty:(ABPropertyID)property fromRecord:(ABRecordRef)record CFRelease(multiValue); return array.copy; } + +- (UIImage *)imagePropertyFullSize:(BOOL)isFullSize fromRecord:(ABRecordRef)recordRef +{ + ABPersonImageFormat format = isFullSize ? kABPersonImageFormatOriginalSize : + kABPersonImageFormatThumbnail; + NSData *data = (__bridge_transfer NSData *)ABPersonCopyImageDataWithFormat(recordRef, format); + return [UIImage imageWithData:data scale:UIScreen.mainScreen.scale]; +} + @end diff --git a/Classes/APTypes.h b/Classes/APTypes.h index 904ced8..cc9a4c6 100644 --- a/Classes/APTypes.h +++ b/Classes/APTypes.h @@ -20,19 +20,20 @@ typedef enum typedef BOOL(^APContactFilterBlock)(APContact *contact); -typedef enum +typedef NS_OPTIONS(NSUInteger , APContactField) { - APContactFieldFirstName = 1 << 0, - APContactFieldLastName = 1 << 1, - APContactFieldCompany = 1 << 2, - APContactFieldPhones = 1 << 3, - APContactFieldEmails = 1 << 4, - APContactFieldPhoto = 1 << 5, - APContactFieldPhotoThumb= 1 << 6, - APContactFieldDefault = APContactFieldFirstName | APContactFieldLastName | - APContactFieldPhones, - APContactFieldAll = APContactFieldDefault | APContactFieldCompany | - APContactFieldEmails | APContactFieldPhoto | APContactFieldPhotoThumb -} APContactField; + APContactFieldFirstName = 1 << 0, + APContactFieldLastName = 1 << 1, + APContactFieldCompany = 1 << 2, + APContactFieldPhones = 1 << 3, + APContactFieldEmails = 1 << 4, + APContactFieldPhoto = 1 << 5, + APContactFieldThumbnail = 1 << 6, + APContactFieldDefault = APContactFieldFirstName | APContactFieldLastName | + APContactFieldPhones, + APContactFieldAll = APContactFieldDefault | APContactFieldCompany | + APContactFieldEmails | APContactFieldPhoto | + APContactFieldThumbnail +}; #endif diff --git a/README.md b/README.md index 1ef3205..f14f481 100644 --- a/README.md +++ b/README.md @@ -42,9 +42,13 @@ Available fields: * APContactFieldPhones - *contact phones array* * APContactFieldEmails - *contact emails array* * APContactFieldPhoto - *contact photo* +* APContactFieldThumbnail - *contact thumbnail* * APContactFieldDefault - *contact first name, last name and phones array* * APContactFieldAll - *all contact fields described above* +### Note +> You should use `APContactFieldPhoto` very carefully, because it takes a lot of memory and may crash the application. Using `APContactFieldThumbnail` is much safer. + Example of loading contact with first name and photo: ```objective-c APAddressBook *addressBook = [[APAddressBook alloc] init]; @@ -97,6 +101,9 @@ If you have improvements or concerns, feel free to post [an issue](https://githu #### Versions +**Version 0.0.3** +* Added loading contact thumbnail. Thanks ti [Carlos Fonseca](https://github.com/carlosefonseca). + **Version 0.0.2** * Fixed potential crash on fetching contacts using non-property APAddressBook object. Thanks to [Evgen Bakumenko](https://github.com/evgenbakumenko).