diff --git a/Signal/src/Profiles/OWSProfileManager.h b/Signal/src/Profiles/OWSProfileManager.h index d460c12b0..558d596fa 100644 --- a/Signal/src/Profiles/OWSProfileManager.h +++ b/Signal/src/Profiles/OWSProfileManager.h @@ -57,6 +57,10 @@ extern NSString *const kNSNotificationName_OtherUsersProfileDidChange; - (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)updateProfileForRecipientId:(NSString *)recipientId diff --git a/Signal/src/Profiles/OWSProfileManager.m b/Signal/src/Profiles/OWSProfileManager.m index f975be3a0..d4e560fa1 100644 --- a/Signal/src/Profiles/OWSProfileManager.m +++ b/Signal/src/Profiles/OWSProfileManager.m @@ -718,6 +718,15 @@ static const NSUInteger kOWSProfileManager_NameDataLength = 26; 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 { OWSAssert([NSThread isMainThread]); @@ -1001,12 +1010,20 @@ static const NSUInteger kOWSProfileManager_NameDataLength = 26; #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]; - UIImage *_Nullable image = [UIImage imageWithContentsOfFile:filePath]; + NSString *filePath = [self.profileAvatarsDirPath stringByAppendingPathComponent:filename]; + 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; } diff --git a/Signal/src/ViewControllers/ContactsViewHelper.m b/Signal/src/ViewControllers/ContactsViewHelper.m index e0b240206..24c3e735c 100644 --- a/Signal/src/ViewControllers/ContactsViewHelper.m +++ b/Signal/src/ViewControllers/ContactsViewHelper.m @@ -412,13 +412,9 @@ NS_ASSUME_NONNULL_BEGIN CNLabeledValue *labeledPhoneNumber = [CNLabeledValue labeledValueWithLabel:CNLabelPhoneNumberMain value:phoneNumber]; newContact.phoneNumbers = @[ labeledPhoneNumber ]; - newContact.givenName = [self.profileManager profileNameForRecipientId:recipientId]; - UIImage *_Nullable profileImage = [self.profileManager profileAvatarForRecipientId:recipientId]; - if (profileImage) { - // TODO get raw jpg data from profileManager to avoid recompress. - newContact.imageData = UIImageJPEGRepresentation(profileImage, 0.9); - } + newContact.givenName = [self.profileManager profileNameForRecipientId:recipientId]; + newContact.imageData = [self.profileManager profileAvatarDataForRecipientId:recipientId]; contactViewController = [CNContactViewController viewControllerForNewContact:newContact]; }