Fastbook should return only squared avatar image for everyone (by cropping non-squared one)

This commit is contained in:
Gautier Pelloux-Prayer 2015-01-16 08:42:14 +01:00
parent 4c33973e00
commit ab7c448a60
3 changed files with 39 additions and 12 deletions

View file

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

View file

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

View file

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