From 4c0dbeb981d5f346b52008b619af8bf7b2d7d0a5 Mon Sep 17 00:00:00 2001 From: Christine Corbett Date: Wed, 31 Dec 2014 18:31:27 +0000 Subject: [PATCH] Group avatar fixes - Resizes group avatars - Fixing bug causing group avatar to be sent as attachment Reviewed-by: @FredericJacobs --- Pods | 2 +- Signal.xcodeproj/project.pbxproj | 4 +- .../Messages/TSMessagesManager+attachments.m | 4 +- Signal/src/util/UIImage+normalizeImage.h | 4 + Signal/src/util/UIImage+normalizeImage.m | 154 ++++++++++++++++++ .../view controllers/NewGroupViewController.m | 6 +- .../PresentIdentityQRCodeViewController.m | 20 +-- 7 files changed, 169 insertions(+), 25 deletions(-) diff --git a/Pods b/Pods index 53c2e1ee8..f90d02274 160000 --- a/Pods +++ b/Pods @@ -1 +1 @@ -Subproject commit 53c2e1ee892acb702b48f16ec8e63a0ea4d0fff4 +Subproject commit f90d02274c5c103da0f13e6e8217b99fb5e1d4d2 diff --git a/Signal.xcodeproj/project.pbxproj b/Signal.xcodeproj/project.pbxproj index e9b8c8c9b..325531081 100644 --- a/Signal.xcodeproj/project.pbxproj +++ b/Signal.xcodeproj/project.pbxproj @@ -3756,7 +3756,7 @@ GCC_DYNAMIC_NO_PIC = NO; GCC_GENERATE_TEST_COVERAGE_FILES = NO; GCC_INSTRUMENT_PROGRAM_FLOW_ARCS = NO; - GCC_OPTIMIZATION_LEVEL = 3; + GCC_OPTIMIZATION_LEVEL = 0; GCC_PREPROCESSOR_DEFINITIONS = ( "DEBUG=1", "$(inherited)", @@ -3876,7 +3876,7 @@ "$(inherited)", "\"$(SRCROOT)\"", ); - GCC_OPTIMIZATION_LEVEL = 3; + GCC_OPTIMIZATION_LEVEL = 0; GCC_PRECOMPILE_PREFIX_HEADER = YES; GCC_PREFIX_HEADER = "Signal/Signal-Prefix.pch"; GCC_PREPROCESSOR_DEFINITIONS = ( diff --git a/Signal/src/textsecure/Messages/TSMessagesManager+attachments.m b/Signal/src/textsecure/Messages/TSMessagesManager+attachments.m index facc9d385..000206c84 100644 --- a/Signal/src/textsecure/Messages/TSMessagesManager+attachments.m +++ b/Signal/src/textsecure/Messages/TSMessagesManager+attachments.m @@ -93,9 +93,9 @@ dispatch_queue_t attachmentsQueue() { [result.pointer saveWithTransaction:transaction]; }]; - TSOutgoingMessage *messageToSend = [[TSOutgoingMessage alloc] initWithTimestamp:outgoingMessage.timeStamp inThread:thread messageBody:@"" attachments:[@[attachementId] mutableCopy]]; + [outgoingMessage.attachments addObject:attachementId]; - [self sendMessage:messageToSend inThread:thread]; + [self sendMessage:outgoingMessage inThread:thread]; } else{ DDLogWarn(@"Failed to upload attachment"); } diff --git a/Signal/src/util/UIImage+normalizeImage.h b/Signal/src/util/UIImage+normalizeImage.h index 6f5746ebe..0669d71ff 100644 --- a/Signal/src/util/UIImage+normalizeImage.h +++ b/Signal/src/util/UIImage+normalizeImage.h @@ -11,5 +11,9 @@ @interface UIImage (normalizeImage) - (UIImage *)normalizedImage; +- (UIImage *)resizedWithQuality:(CGInterpolationQuality)quality rate:(CGFloat)rate; + +-(UIImage*)resizedImageToSize:(CGSize)dstSize; +-(UIImage*)resizedImageToFitInSize:(CGSize)boundingSize scaleIfSmaller:(BOOL)scale; @end diff --git a/Signal/src/util/UIImage+normalizeImage.m b/Signal/src/util/UIImage+normalizeImage.m index 69f812cfd..74593bae2 100644 --- a/Signal/src/util/UIImage+normalizeImage.m +++ b/Signal/src/util/UIImage+normalizeImage.m @@ -20,4 +20,158 @@ return normalizedImage; } + +- (UIImage *)resizedWithQuality:(CGInterpolationQuality)quality rate:(CGFloat)rate { + UIImage *resized = nil; + CGFloat width = self.size.width * rate; + CGFloat height = self.size.height * rate; + + UIGraphicsBeginImageContext(CGSizeMake(width, height)); + CGContextRef context = UIGraphicsGetCurrentContext(); + CGContextSetInterpolationQuality(context, quality); + [self drawInRect:CGRectMake(0, 0, width, height)]; + resized = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + return resized; +} + +// Source: https://github.com/AliSoftware/UIImage-Resize + +-(UIImage*)resizedImageToSize:(CGSize)dstSize +{ + CGImageRef imgRef = self.CGImage; + // the below values are regardless of orientation : for UIImages from Camera, width>height (landscape) + CGSize srcSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); // not equivalent to self.size (which is dependant on the imageOrientation)! + + /* Don't resize if we already meet the required destination size. */ + if (CGSizeEqualToSize(srcSize, dstSize)) { + return self; + } + + CGFloat scaleRatio = dstSize.width / srcSize.width; + UIImageOrientation orient = self.imageOrientation; + CGAffineTransform transform = CGAffineTransformIdentity; + switch(orient) { + + case UIImageOrientationUp: //EXIF = 1 + transform = CGAffineTransformIdentity; + break; + + case UIImageOrientationUpMirrored: //EXIF = 2 + transform = CGAffineTransformMakeTranslation(srcSize.width, 0.0); + transform = CGAffineTransformScale(transform, -1.0, 1.0); + break; + + case UIImageOrientationDown: //EXIF = 3 + transform = CGAffineTransformMakeTranslation(srcSize.width, srcSize.height); + transform = CGAffineTransformRotate(transform, M_PI); + break; + + case UIImageOrientationDownMirrored: //EXIF = 4 + transform = CGAffineTransformMakeTranslation(0.0, srcSize.height); + transform = CGAffineTransformScale(transform, 1.0, -1.0); + break; + + case UIImageOrientationLeftMirrored: //EXIF = 5 + dstSize = CGSizeMake(dstSize.height, dstSize.width); + transform = CGAffineTransformMakeTranslation(srcSize.height, srcSize.width); + transform = CGAffineTransformScale(transform, -1.0, 1.0); + transform = CGAffineTransformRotate(transform, 3.0 * M_PI_2); + break; + + case UIImageOrientationLeft: //EXIF = 6 + dstSize = CGSizeMake(dstSize.height, dstSize.width); + transform = CGAffineTransformMakeTranslation(0.0, srcSize.width); + transform = CGAffineTransformRotate(transform, 3.0 * M_PI_2); + break; + + case UIImageOrientationRightMirrored: //EXIF = 7 + dstSize = CGSizeMake(dstSize.height, dstSize.width); + transform = CGAffineTransformMakeScale(-1.0, 1.0); + transform = CGAffineTransformRotate(transform, M_PI_2); + break; + + case UIImageOrientationRight: //EXIF = 8 + dstSize = CGSizeMake(dstSize.height, dstSize.width); + transform = CGAffineTransformMakeTranslation(srcSize.height, 0.0); + transform = CGAffineTransformRotate(transform, M_PI_2); + break; + + default: + [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"]; + + } + + ///////////////////////////////////////////////////////////////////////////// + // The actual resize: draw the image on a new context, applying a transform matrix + UIGraphicsBeginImageContextWithOptions(dstSize, NO, self.scale); + + CGContextRef context = UIGraphicsGetCurrentContext(); + + if (!context) { + return nil; + } + + if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { + CGContextScaleCTM(context, -scaleRatio, scaleRatio); + CGContextTranslateCTM(context, -srcSize.height, 0); + } else { + CGContextScaleCTM(context, scaleRatio, -scaleRatio); + CGContextTranslateCTM(context, 0, -srcSize.height); + } + + CGContextConcatCTM(context, transform); + + // we use srcSize (and not dstSize) as the size to specify is in user space (and we use the CTM to apply a scaleRatio) + CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, srcSize.width, srcSize.height), imgRef); + UIImage* resizedImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + return resizedImage; +} + +-(UIImage*)resizedImageToFitInSize:(CGSize)boundingSize scaleIfSmaller:(BOOL)scale +{ + // get the image size (independant of imageOrientation) + CGImageRef imgRef = self.CGImage; + CGSize srcSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); // not equivalent to self.size (which depends on the imageOrientation)! + + // adjust boundingSize to make it independant on imageOrientation too for farther computations + UIImageOrientation orient = self.imageOrientation; + switch (orient) { + case UIImageOrientationLeft: + case UIImageOrientationRight: + case UIImageOrientationLeftMirrored: + case UIImageOrientationRightMirrored: + boundingSize = CGSizeMake(boundingSize.height, boundingSize.width); + break; + default: + // NOP + break; + } + + // Compute the target CGRect in order to keep aspect-ratio + CGSize dstSize; + + if ( !scale && (srcSize.width < boundingSize.width) && (srcSize.height < boundingSize.height) ) { + //NSLog(@"Image is smaller, and we asked not to scale it in this case (scaleIfSmaller:NO)"); + dstSize = srcSize; // no resize (we could directly return 'self' here, but we draw the image anyway to take image orientation into account) + } else { + CGFloat wRatio = boundingSize.width / srcSize.width; + CGFloat hRatio = boundingSize.height / srcSize.height; + + if (wRatio < hRatio) { + //NSLog(@"Width imposed, Height scaled ; ratio = %f",wRatio); + dstSize = CGSizeMake(boundingSize.width, floor(srcSize.height * wRatio)); + } else { + //NSLog(@"Height imposed, Width scaled ; ratio = %f",hRatio); + dstSize = CGSizeMake(floor(srcSize.width * hRatio), boundingSize.height); + } + } + + return [self resizedImageToSize:dstSize]; +} + + @end diff --git a/Signal/src/view controllers/NewGroupViewController.m b/Signal/src/view controllers/NewGroupViewController.m index 62bdfa852..3bfd5613c 100644 --- a/Signal/src/view controllers/NewGroupViewController.m +++ b/Signal/src/view controllers/NewGroupViewController.m @@ -19,6 +19,8 @@ #import "SecurityUtils.h" #import "SignalKeyingStorage.h" +#import "UIImage+normalizeImage.h" + #import "UIUtil.h" #import "DJWActionSheet.h" #import @@ -131,7 +133,6 @@ static NSString* const kUnwindToMessagesViewSegue = @"UnwindToMessagesViewSegue" _groupModel = [[GroupModel alloc] initWithTitle:_nameGroupTextField.text memberIds:[NSMutableArray arrayWithArray:[[NSSet setWithArray:mut] allObjects]] image:_groupImageButton.imageView.image groupId:_thread.groupModel.groupId]; [self performSegueWithIdentifier:kUnwindToMessagesViewSegue sender:self]; - } @@ -223,8 +224,7 @@ static NSString* const kUnwindToMessagesViewSegue = @"UnwindToMessagesViewSegue" UIImage *picture_camera = [info objectForKey:UIImagePickerControllerOriginalImage]; if (picture_camera) { - [self setupGroupImageButton:picture_camera]; - + [self setupGroupImageButton:[picture_camera resizedImageToFitInSize:CGSizeMake(100.0,100.0) scaleIfSmaller:NO]]; } [self dismissViewControllerAnimated:YES completion:nil]; } diff --git a/Signal/src/view controllers/PresentIdentityQRCodeViewController.m b/Signal/src/view controllers/PresentIdentityQRCodeViewController.m index 6dc00f9ca..9134034e1 100644 --- a/Signal/src/view controllers/PresentIdentityQRCodeViewController.m +++ b/Signal/src/view controllers/PresentIdentityQRCodeViewController.m @@ -8,6 +8,8 @@ #import "PresentIdentityQRCodeViewController.h" #import "NSData+Base64.h" +#import "UIImage+normalizeImage.h" + @implementation PresentIdentityQRCodeViewController @@ -36,7 +38,7 @@ UIImage *image = [UIImage imageWithCGImage:cgImage scale:1. orientation:UIImageOrientationUp]; // Resize without interpolating - UIImage *resized = [self resizeImage:image withQuality:kCGInterpolationNone rate:5.0]; + UIImage *resized = [image resizedWithQuality:kCGInterpolationNone rate:5.0]; self.qrCodeView.image = resized; @@ -55,21 +57,5 @@ } -#pragma mark - Private - -- (UIImage *)resizeImage:(UIImage *)image withQuality:(CGInterpolationQuality)quality rate:(CGFloat)rate { - UIImage *resized = nil; - CGFloat width = image.size.width * rate; - CGFloat height = image.size.height * rate; - - UIGraphicsBeginImageContext(CGSizeMake(width, height)); - CGContextRef context = UIGraphicsGetCurrentContext(); - CGContextSetInterpolationQuality(context, quality); - [image drawInRect:CGRectMake(0, 0, width, height)]; - resized = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); - - return resized; -} @end