From ab7c448a6011208abf68073ab87da52d481d6f9a Mon Sep 17 00:00:00 2001 From: Gautier Pelloux-Prayer Date: Fri, 16 Jan 2015 08:42:14 +0100 Subject: [PATCH] Fastbook should return only squared avatar image for everyone (by cropping non-squared one) --- Classes/LinphoneUI/UIContactDetailsHeader.m | 2 +- Classes/LinphoneUI/UIRoundedImageView.m | 14 ++------- Classes/Utils/FastAddressBook.m | 35 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/Classes/LinphoneUI/UIContactDetailsHeader.m b/Classes/LinphoneUI/UIContactDetailsHeader.m index 090a4dd34..be4d05af9 100644 --- a/Classes/LinphoneUI/UIContactDetailsHeader.m +++ b/Classes/LinphoneUI/UIContactDetailsHeader.m @@ -123,7 +123,7 @@ // Avatar image { - UIImage *image = [FastAddressBook getContactImage:contact thumbnail:true]; + UIImage *image = [FastAddressBook getContactImage:contact thumbnail:false]; if(image == nil) { image = [UIImage imageNamed:@"avatar_unknown_small.png"]; } diff --git a/Classes/LinphoneUI/UIRoundedImageView.m b/Classes/LinphoneUI/UIRoundedImageView.m index b3e7ce8a3..983b9570c 100644 --- a/Classes/LinphoneUI/UIRoundedImageView.m +++ b/Classes/LinphoneUI/UIRoundedImageView.m @@ -29,21 +29,13 @@ [self setRoundRadius:rounded]; } +// warning: for non-squared image, this function will generate an ellipsoidal image, not a round image! - (void)setRoundRadius:(BOOL)radius { CALayer *imageLayer = self.layer; - CGRect frame = imageLayer.frame; - CGFloat height =self.frame.size.height; - CGFloat width = self.frame.size.width; + CGFloat height = frame.size.height; + CGFloat width = frame.size.width; CGFloat roundRadius = height > width ? width / 2 : height / 2; - if (height > width) { - frame.origin.y = height / 2 - width / 2; - frame.size.height = width; - } else { - frame.origin.x = width / 2 - height / 2; - frame.size.width = height; - } - [imageLayer setFrame:frame]; [imageLayer setCornerRadius:roundRadius]; [imageLayer setBorderWidth:0]; [imageLayer setMasksToBounds:YES]; diff --git a/Classes/Utils/FastAddressBook.m b/Classes/Utils/FastAddressBook.m index 0fe380de8..35fb6d6ec 100644 --- a/Classes/Utils/FastAddressBook.m +++ b/Classes/Utils/FastAddressBook.m @@ -36,6 +36,35 @@ static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef inf return retString; } ++ (UIImage*)squareImageCrop:(UIImage*)image +{ + UIImage *ret = nil; + + // This calculates the crop area. + + float originalWidth = image.size.width; + float originalHeight = image.size.height; + + float edge = fminf(originalWidth, originalHeight); + + float posX = (originalWidth - edge) / 2.0f; + float posY = (originalHeight - edge) / 2.0f; + + + CGRect cropSquare = CGRectMake(posX, posY, + edge, edge); + + + CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropSquare); + ret = [UIImage imageWithCGImage:imageRef + scale:image.scale + orientation:image.imageOrientation]; + + CGImageRelease(imageRef); + + return ret; +} + + (UIImage*)getContactImage:(ABRecordRef)contact thumbnail:(BOOL)thumbnail { UIImage* retImage = nil; if (contact && ABPersonHasImageData(contact)) { @@ -46,7 +75,13 @@ static void sync_address_book (ABAddressBookRef addressBook, CFDictionaryRef inf if(imgData != NULL) { CFRelease(imgData); } + + if (retImage != nil && retImage.size.width != retImage.size.height) { + [LinphoneLogger log:LinphoneLoggerLog format:@"Image is not square : cropping it."]; + return [self squareImageCrop:retImage]; + } } + return retImage; }