Skip to content

Commit

Permalink
Refactored thumnail loading
Browse files Browse the repository at this point in the history
Updated readme
  • Loading branch information
belkevich committed Jan 20, 2014
1 parent 937bec5 commit 9c9596e
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion AddressBook/AddressBook/ContactTableViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Classes/APContact.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
17 changes: 12 additions & 5 deletions Classes/APContact.m
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
27 changes: 14 additions & 13 deletions Classes/APTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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).

Expand Down

0 comments on commit 9c9596e

Please sign in to comment.