moved resizeImage method to utils

This commit is contained in:
David Idmansour 2018-06-18 10:11:05 +02:00
parent 5b6e9f6edb
commit 48f717f0ae
5 changed files with 32 additions and 36 deletions

View file

@ -167,39 +167,6 @@
}
}
+ (UIImage *)resizeImage:(UIImage *)image
{
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float maxHeight = 200.0;
float maxWidth = 200.0;
float imgRatio = actualWidth/actualHeight;
float maxRatio = maxWidth/maxHeight;
float compressionQuality = 1;
if (actualHeight > maxHeight || actualWidth > maxWidth)
{
if(imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
} else if(imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
} else {
actualHeight = maxHeight;
actualWidth = maxWidth;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
UIGraphicsEndImageContext();
return [UIImage imageWithData:imageData];
}
+ (void) saveDataToUserDefaults {
const bctbx_list_t *logs = linphone_core_get_call_logs(LC);
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"group.belledonne-communications.linphone.widget"];
@ -215,7 +182,7 @@
//FastAddressBook *fab = [LinphoneManager instance].fastAddressBook;
Contact * contact = [FastAddressBook getContactWithAddress:linphone_call_log_get_remote_address(log)];
if (contact && contact.avatar) {
UIImage *image = [self resizeImage:contact.avatar];
UIImage *image = [UIImage resizeImage:contact.avatar withMaxWidth:200 andMaxHeight:200];
NSData *imageData = UIImageJPEGRepresentation(image, 0);
[images setObject:imageData
forKey:[[NSString stringWithUTF8String:linphone_address_as_string_uri_only(linphone_call_log_get_remote_address(log))] substringFromIndex:4]];

View file

@ -68,6 +68,7 @@ typedef enum {
- (UIImage *)squareCrop;
- (UIImage *)scaleToSize:(CGSize)size squared:(BOOL)squared;
+ (UIImage *)resizeImage:(UIImage *)image withMaxWidth:(float)maxWidth andMaxHeight:(float)maxHeight;
@end

View file

@ -644,6 +644,36 @@
return scaledImage;
}
+ (UIImage *)resizeImage:(UIImage *)image withMaxWidth:(float)maxWidth andMaxHeight:(float)maxHeight {
float actualHeight = image.size.height;
float actualWidth = image.size.width;
float imgRatio = actualWidth / actualHeight;
float maxRatio = maxWidth / maxHeight;
float compressionQuality = 1;
if (actualHeight > maxHeight || actualWidth > maxWidth)
{
if (imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = imgRatio * actualWidth;
actualHeight = maxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = imgRatio * actualHeight;
actualWidth = maxWidth;
} else {
actualHeight = maxHeight;
actualWidth = maxWidth;
}
}
CGRect rect = CGRectMake(0.0, 0.0, actualWidth, actualHeight);
UIGraphicsBeginImageContext(rect.size);
[image drawInRect:rect];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
NSData *imageData = UIImageJPEGRepresentation(img, compressionQuality);
UIGraphicsEndImageContext();
return [UIImage imageWithData:imageData];
}
@end
@implementation UIColor (LightAndDark)

View file

@ -14,7 +14,6 @@
@property (strong, nonatomic) NSMutableDictionary *imgs;
@property (strong, nonatomic) NSMutableArray *sortedDates;
@property (strong, nonatomic) NSMutableArray *logIds;
@property (nonatomic) BOOL updateNeeded;
- (IBAction)firstButtonTapped;
- (IBAction)secondButtonTapped;

View file

@ -131,7 +131,6 @@
_sortedDates = [NSMutableArray array];
_contactsToDisplay = [NSMutableArray array];
_imgs = [NSMutableDictionary dictionary];
_updateNeeded = YES;
}
- (void)didReceiveMemoryWarning {