Avoid lossy re-encoding of profile image

// FREEBIE
pull/1/head
Michael Kirk 8 years ago
parent 0290f176c0
commit 9f72db44ac

@ -57,6 +57,10 @@ extern NSString *const kNSNotificationName_OtherUsersProfileDidChange;
- (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId; - (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId;
// Reads raw avatar data from disk if available. Uncached, so shouldn't be used frequently,
// but useful to get the raw image data for populating cnContact.imageData without lossily re-encoding.
- (nullable NSData *)profileAvatarDataForRecipientId:(NSString *)recipientId;
- (void)refreshProfileForRecipientId:(NSString *)recipientId; - (void)refreshProfileForRecipientId:(NSString *)recipientId;
- (void)updateProfileForRecipientId:(NSString *)recipientId - (void)updateProfileForRecipientId:(NSString *)recipientId

@ -718,6 +718,15 @@ static const NSUInteger kOWSProfileManager_NameDataLength = 26;
return userProfile.profileName; return userProfile.profileName;
} }
- (nullable NSData *)profileAvatarDataForRecipientId:(NSString *)recipientId
{
UserProfile *userProfile = [self getOrBuildUserProfileForRecipientId:recipientId];
if (userProfile.avatarFileName.length > 0) {
return [self loadProfileDataWithFilename:userProfile.avatarFileName];
}
return nil;
}
- (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId - (nullable UIImage *)profileAvatarForRecipientId:(NSString *)recipientId
{ {
OWSAssert([NSThread isMainThread]); OWSAssert([NSThread isMainThread]);
@ -1001,12 +1010,20 @@ static const NSUInteger kOWSProfileManager_NameDataLength = 26;
#pragma mark - Avatar Disk Cache #pragma mark - Avatar Disk Cache
- (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)fileName - (nullable NSData *)loadProfileDataWithFilename:(NSString *)filename
{ {
OWSAssert(fileName.length > 0); OWSAssert(filename.length > 0);
NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:fileName]; NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:filename];
UIImage *_Nullable image = [UIImage imageWithContentsOfFile:filePath]; return [NSData dataWithContentsOfFile:filePath];
}
- (nullable UIImage *)loadProfileAvatarWithFilename:(NSString *)filename
{
OWSAssert(filename.length > 0);
NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:filename];
UIImage *_Nullable image = [UIImage imageWithData:[self loadProfileDataWithFilename:filename]];
return image; return image;
} }

@ -412,13 +412,9 @@ NS_ASSUME_NONNULL_BEGIN
CNLabeledValue<CNPhoneNumber *> *labeledPhoneNumber = CNLabeledValue<CNPhoneNumber *> *labeledPhoneNumber =
[CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMain value:phoneNumber]; [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMain value:phoneNumber];
newContact.phoneNumbers = @[ labeledPhoneNumber ]; newContact.phoneNumbers = @[ labeledPhoneNumber ];
newContact.givenName = [self.profileManager profileNameForRecipientId:recipientId];
UIImage *_Nullable profileImage = [self.profileManager profileAvatarForRecipientId:recipientId]; newContact.givenName = [self.profileManager profileNameForRecipientId:recipientId];
if (profileImage) { newContact.imageData = [self.profileManager profileAvatarDataForRecipientId:recipientId];
// TODO get raw jpg data from profileManager to avoid recompress.
newContact.imageData = UIImageJPEGRepresentation(profileImage, 0.9);
}
contactViewController = [CNContactViewController viewControllerForNewContact:newContact]; contactViewController = [CNContactViewController viewControllerForNewContact:newContact];
} }

Loading…
Cancel
Save