diff --git a/Signal/src/Storyboard/Storyboard.storyboard b/Signal/src/Storyboard/Storyboard.storyboard index f66f3789a..540cdd7c6 100755 --- a/Signal/src/Storyboard/Storyboard.storyboard +++ b/Signal/src/Storyboard/Storyboard.storyboard @@ -1,5 +1,5 @@ - + @@ -2887,6 +2887,104 @@ Licensed under the GPLv3 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -3416,7 +3514,7 @@ Licensed under the GPLv3 - + diff --git a/Signal/src/environment/PreferencesUtil.h b/Signal/src/environment/PreferencesUtil.h index b5271ce26..0a810a482 100644 --- a/Signal/src/environment/PreferencesUtil.h +++ b/Signal/src/environment/PreferencesUtil.h @@ -9,6 +9,12 @@ typedef NS_ENUM(NSUInteger, NotificationType) { NotificationNamePreview, }; +typedef NS_ENUM(NSUInteger, TSImageQuality) { + TSImageQualityHigh, + TSImageQualityMedium, + TSImageQualityLow +}; + @class PhoneNumber; @interface PropertyListPreferences (PropertyUtil) @@ -27,6 +33,9 @@ typedef NS_ENUM(NSUInteger, NotificationType) { -(NotificationType)notificationPreviewType; -(void)setNotificationPreviewType:(NotificationType)type; +-(TSImageQuality)imageUploadQuality; +-(void)setImageUploadQuality:(TSImageQuality)quality; + -(NSString*)lastRanVersion; -(NSString*)setAndGetCurrentVersion; diff --git a/Signal/src/environment/PreferencesUtil.m b/Signal/src/environment/PreferencesUtil.m index 192ff1754..3ccd0d3b9 100644 --- a/Signal/src/environment/PreferencesUtil.m +++ b/Signal/src/environment/PreferencesUtil.m @@ -24,6 +24,7 @@ #define SCREEN_SECURITY_KEY @"Screen Security Key" #define DEBUG_IS_ENABLED_KEY @"Debugging Log Enabled Key" #define NOTIFICATION_PREVIEW_TYPE_KEY @"Notification Preview Type Key" +#define IMAGE_UPLOAD_QUALITY_KEY @"Image Upload Quality Key" #define kSignalVersionKey @"SignalUpdateVersionKey" @@ -128,6 +129,20 @@ } } +-(TSImageQuality)imageUploadQuality { + NSNumber * preference = [self tryGetValueForKey:IMAGE_UPLOAD_QUALITY_KEY]; + + if (preference) { + return [preference unsignedIntegerValue]; + } else { + return TSImageQualityMedium; + } +} + +-(void)setImageUploadQuality:(TSImageQuality)quality { + [self setValueForKey:IMAGE_UPLOAD_QUALITY_KEY toValue:@(quality)]; +} + -(void)setNotificationPreviewType:(NotificationType)type { [self setValueForKey:NOTIFICATION_PREVIEW_TYPE_KEY toValue:@(type)]; diff --git a/Signal/src/view controllers/MessagesViewController.m b/Signal/src/view controllers/MessagesViewController.m index b1bcdb3a5..5dcda79d5 100644 --- a/Signal/src/view controllers/MessagesViewController.m +++ b/Signal/src/view controllers/MessagesViewController.m @@ -48,6 +48,7 @@ #import "Environment.h" #import "PhoneManager.h" #import "ContactsManager.h" +#import "PreferencesUtil.h" static NSTimeInterval const kTSMessageSentDateShowTimeInterval = 5 * 60; static NSString *const kUpdateGroupSegueIdentifier = @"updateGroupSegue"; @@ -662,13 +663,78 @@ typedef enum : NSUInteger { DDLogWarn(@"Video formats not supported, yet"); } else if (picture_camera) { DDLogVerbose(@"Sending picture attachement ..."); - [[TSMessagesManager sharedManager] sendAttachment:UIImagePNGRepresentation(picture_camera) contentType:@"image/png" thread:self.thread]; + [[TSMessagesManager sharedManager] sendAttachment:[self qualityAdjustedAttachmentForImage:picture_camera] contentType:@"image/jpeg" thread:self.thread]; + [self finishSendingMessage]; } [self dismissViewControllerAnimated:YES completion:nil]; } +-(NSData*)qualityAdjustedAttachmentForImage:(UIImage*)image +{ + return UIImageJPEGRepresentation([self adjustedImageSizedForSending:image], [self compressionRate]); +} + +-(UIImage*)adjustedImageSizedForSending:(UIImage*)image +{ + CGFloat correctedWidth; + switch ([Environment.preferences imageUploadQuality]) { + case TSImageQualityHigh: + correctedWidth = 2048; + break; + case TSImageQualityMedium: + correctedWidth = 1024; + break; + case TSImageQualityLow: + correctedWidth = 512; + break; + default: + break; + } + + return [self imageScaled:image toMaxSize:correctedWidth]; +} + +- (UIImage*)imageScaled:(UIImage *)image toMaxSize:(CGFloat)size +{ + CGFloat scaleFactor; + CGFloat aspectRatio = image.size.height / image.size.width; + + if( aspectRatio > 1 ) { + scaleFactor = size / image.size.width; + } + else { + scaleFactor = size / image.size.height; + } + + CGSize newSize = CGSizeMake(image.size.width * scaleFactor, image.size.height * scaleFactor); + + UIGraphicsBeginImageContext(newSize); + [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; + UIImage* updatedImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + return updatedImage; +} + +-(CGFloat)compressionRate +{ + switch ([Environment.preferences imageUploadQuality]) { + case TSImageQualityHigh: + return 0.9f; + break; + case TSImageQualityMedium: + return 0.5f; + break; + case TSImageQualityLow: + return 0.3f; + break; + default: + break; + } +} + #pragma mark Storage access - (YapDatabaseConnection*)uiDatabaseConnection { diff --git a/Signal/src/view controllers/SettingsTableViewCell.h b/Signal/src/view controllers/SettingsTableViewCell.h index a040e4bfe..30222e092 100644 --- a/Signal/src/view controllers/SettingsTableViewCell.h +++ b/Signal/src/view controllers/SettingsTableViewCell.h @@ -10,7 +10,10 @@ @interface SettingsTableViewCell : UITableViewCell -//Regular cell +-(void)updateImageQualityLabel; + +//Optionals @property(nonatomic, strong) IBOutlet UISwitch* toggle; +@property(nonatomic,strong) IBOutlet UILabel* detailLabel; @end diff --git a/Signal/src/view controllers/SettingsTableViewCell.m b/Signal/src/view controllers/SettingsTableViewCell.m index 86dfa590c..65a7fbc82 100644 --- a/Signal/src/view controllers/SettingsTableViewCell.m +++ b/Signal/src/view controllers/SettingsTableViewCell.m @@ -14,16 +14,19 @@ @implementation SettingsTableViewCell - (void)awakeFromNib { - // Initialization code - [self.toggle setOn:[Environment.preferences screenSecurityIsEnabled]]; - [self.toggle addTarget:self action:@selector(toggleSetting:) forControlEvents:UIControlEventValueChanged]; + if (self.toggle) { + [self.toggle setOn:[Environment.preferences screenSecurityIsEnabled]]; + [self.toggle addTarget:self action:@selector(toggleSetting:) forControlEvents:UIControlEventValueChanged]; + } + + if ([self.reuseIdentifier isEqualToString:@"imageUploadQuality"]) { + [self updateImageQualityLabel]; + } } - (void)setSelected:(BOOL)selected animated:(BOOL)animated { [super setSelected:selected animated:animated]; - - // Configure the view for the selected state } #pragma mark - UISwitch @@ -35,5 +38,24 @@ } } +#pragma mark - Detail Label + +-(void)updateImageQualityLabel +{ + switch ([Environment.preferences imageUploadQuality]) { + case TSImageQualityHigh: + self.detailLabel.text = @"High"; + break; + case TSImageQualityMedium: + self.detailLabel.text = @"Medium"; + break; + case TSImageQualityLow: + self.detailLabel.text = @"Low"; + break; + default: + DDLogWarn(@"Unknown Image Quality setting : %ld <%s>", [Environment.preferences imageUploadQuality], __PRETTY_FUNCTION__); + break; + } +} @end diff --git a/Signal/src/view controllers/SettingsTableViewController.m b/Signal/src/view controllers/SettingsTableViewController.m index 55870e19a..471932411 100644 --- a/Signal/src/view controllers/SettingsTableViewController.m +++ b/Signal/src/view controllers/SettingsTableViewController.m @@ -12,6 +12,8 @@ #import "TSAccountManager.h" #import "TSStorageManager.h" +#import "Environment.h" +#import "PreferencesUtil.h" #import "RPServerRequestsManager.h" @@ -22,15 +24,16 @@ #define kNumberOfSections 2 -#define kClearHistoryLogCellRow 2 -#define kMessageDisplayCellRow 3 -#define kSendDebugLogCellRow 4 -#define kUnregisterCell 5 +#define kMessageDisplayCellRow 1 +#define kImageQualitySettingRow 2 +#define kClearHistoryLogCellRow 3 +#define kSendDebugLogCellRow 5 +#define kUnregisterCell 6 typedef enum { kProfileRows = 1, - kSecurityRows = 6, + kSecurityRows = 7, } kRowsForSection; typedef enum { @@ -115,6 +118,43 @@ typedef enum { break; } + + case kImageQualitySettingRow: + { + [DJWActionSheet showInView:self.tabBarController.view + withTitle:nil + cancelButtonTitle:@"Cancel" + destructiveButtonTitle:nil + otherButtonTitles:@[@"High", @"Medium", @"Low"] + tapBlock:^(DJWActionSheet *actionSheet, NSInteger tappedButtonIndex) { + [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; + if (tappedButtonIndex == actionSheet.cancelButtonIndex) { + DDLogVerbose(@"User Cancelled <%s>", __PRETTY_FUNCTION__); + + } else if (tappedButtonIndex == actionSheet.destructiveButtonIndex) { + DDLogVerbose(@"Destructive button tapped <%s>", __PRETTY_FUNCTION__); + }else { + switch (tappedButtonIndex) { + case 0: + [Environment.preferences setImageUploadQuality:TSImageQualityHigh]; + break; + case 1: + [Environment.preferences setImageUploadQuality:TSImageQualityMedium]; + break; + case 2: + [Environment.preferences setImageUploadQuality:TSImageQualityLow]; + break; + default: + DDLogWarn(@"Illegal Image Quality Tapped in <%s>", __PRETTY_FUNCTION__); + break; + } + + SettingsTableViewCell * cell = (SettingsTableViewCell*)[tableView cellForRowAtIndexPath:indexPath]; + [cell updateImageQualityLabel]; + } + }]; + break; + } case kSendDebugLogCellRow: [Pastelog submitLogs];