Avoid lossy re-encoding of profile image

// FREEBIE
This commit is contained in:
Michael Kirk 2017-08-15 12:49:27 -04:00
parent 0290f176c0
commit 9f72db44ac
3 changed files with 27 additions and 10 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -412,13 +412,9 @@ NS_ASSUME_NONNULL_BEGIN
CNLabeledValue<CNPhoneNumber *> *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];
}