diff --git a/Classes/ChatConversationTableView.h b/Classes/ChatConversationTableView.h index 63106f8a6..aae766894 100644 --- a/Classes/ChatConversationTableView.h +++ b/Classes/ChatConversationTableView.h @@ -40,7 +40,6 @@ @property(nonatomic) LinphoneChatRoom *chatRoom; @property(nonatomic, strong) id chatRoomDelegate; -@property(nonatomic) NSMutableDictionary *photoSizesDict; - (void)addEventEntry:(LinphoneEventLog *)event; - (void)scrollToBottom:(BOOL)animated; diff --git a/Classes/ChatConversationTableView.m b/Classes/ChatConversationTableView.m index 658a7d459..f529031c5 100644 --- a/Classes/ChatConversationTableView.m +++ b/Classes/ChatConversationTableView.m @@ -38,7 +38,6 @@ - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; - self.photoSizesDict = [NSMutableDictionary dictionary]; self.tableView.accessibilityIdentifier = @"ChatRoom list"; } @@ -179,15 +178,11 @@ kCellId = NSStringFromClass(UIChatBubblePhotoCell.class); else kCellId = NSStringFromClass(UIChatBubbleTextCell.class); + UIChatBubbleTextCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellId]; if (!cell) { cell = [[NSClassFromString(kCellId) alloc] initWithIdentifier:kCellId]; - cell.indexInTable = indexPath; } - //if (linphone_chat_message_get_file_transfer_information(chat) || linphone_chat_message_get_external_body_url(chat)) { - // UIChatBubblePhotoCell *photoCell = (UIChatBubblePhotoCell *) cell; - // [photoCell unloadImages]; - //} [cell setEvent:event]; if (chat) [cell update]; @@ -220,14 +215,9 @@ if (linphone_event_log_get_type(event) == LinphoneEventLogTypeConferenceChatMessage) { LinphoneChatMessage *chat = linphone_event_log_get_chat_message(event); - if ((linphone_chat_message_get_file_transfer_information(chat) || linphone_chat_message_get_external_body_url(chat)) && [_photoSizesDict objectForKey:indexPath]) { - return [_photoSizesDict objectForKey:indexPath].doubleValue; - } - return [UIChatBubbleTextCell ViewHeightForMessage:chat withWidth:self.view.frame.size.width].height; - } else { - return [UIChatNotifiedEventCell height]; } + return [UIChatNotifiedEventCell height]; } - (void) tableView:(UITableView *)tableView deleteRowAtIndex:(NSIndexPath *)indexPath { diff --git a/Classes/LinphoneUI/Base.lproj/UIChatBubblePhotoCell.xib b/Classes/LinphoneUI/Base.lproj/UIChatBubblePhotoCell.xib index ca3281610..5762ee1c6 100644 --- a/Classes/LinphoneUI/Base.lproj/UIChatBubblePhotoCell.xib +++ b/Classes/LinphoneUI/Base.lproj/UIChatBubblePhotoCell.xib @@ -85,13 +85,6 @@ - @@ -100,9 +93,6 @@ - - - @@ -141,6 +131,20 @@ + + + + + + + @@ -152,16 +156,16 @@ - - - - - + + + + + diff --git a/Classes/LinphoneUI/UIChatBubblePhotoCell.m b/Classes/LinphoneUI/UIChatBubblePhotoCell.m index ba0aed618..4f3c0c3bd 100644 --- a/Classes/LinphoneUI/UIChatBubblePhotoCell.m +++ b/Classes/LinphoneUI/UIChatBubblePhotoCell.m @@ -26,10 +26,10 @@ @implementation UIChatBubblePhotoCell { FileTransferDelegate *_ftd; - bool isAssetLoaded; CGSize imageSize, bubbleSize; int actualAvailableWidth; - CGImageRef displayedImage; + ChatConversationTableView *chatTableView; + //CGImageRef displayedImage; } #pragma mark - Lifecycle Functions @@ -48,14 +48,11 @@ break; } } - // Useless - //[self setFrame:CGRectMake(0, 0, 5, 100)]; - //self.autoresizesSubviews = TRUE; + [self setFrame:CGRectMake(0, 0, 5, 100)]; [self addSubview:sub]; - isAssetLoaded = FALSE; - actualAvailableWidth = self.frame.size.width; - [self setBubbleSize]; - LOGD(@"ZBOUB"); + //LOGD(@"ZBOUB"); + chatTableView = VIEW(ChatConversationView).tableController; + actualAvailableWidth = chatTableView.tableView.frame.size.width; } return self; } @@ -67,20 +64,20 @@ super.event = event; [self setChatMessage:linphone_event_log_get_chat_message(event)]; - [VIEW(ChatConversationView).tableController.photoSizesDict setObject:@(bubbleSize.height) forKey:self.indexInTable]; } - (void)setChatMessage:(LinphoneChatMessage *)amessage { _imageGestureRecognizer.enabled = NO; _messageImageView.image = nil; - //_finalImage.image = nil; - //_finalImage. = TRUE; + _finalImage.image = nil; + _finalImage.hidden = TRUE; _fileTransferProgress.progress = 0; [self disconnectFromFileDelegate]; if (amessage) { const LinphoneContent *c = linphone_chat_message_get_file_transfer_information(amessage); if (c) { + const char *name = linphone_content_get_name(c); for (FileTransferDelegate *aftd in [LinphoneManager.instance fileTransferDelegates]) { if (linphone_chat_message_get_file_transfer_information(aftd.message) && @@ -99,26 +96,21 @@ } - (void) loadAsset:(ALAsset*) asset { - if (isAssetLoaded) return; - LOGD(@"BITEENBOIS"); - isAssetLoaded = TRUE; + UIImage *thumb = [[UIImage alloc] initWithCGImage:[asset thumbnail]]; + ALAssetRepresentation *representation = [asset defaultRepresentation]; + imageSize = [UIChatBubbleTextCell getMediaMessageSizefromOriginalSize:[representation dimensions] withWidth:chatTableView.tableView.frame.size.width]; + CGImageRef tmpImg = [self cropImageFromRepresentation:representation]; + UIImage *image = [[UIImage alloc] initWithCGImage:tmpImg]; dispatch_async(dispatch_get_main_queue(), ^{ - ALAssetRepresentation *representation = [asset defaultRepresentation]; - imageSize = [self getMediaMessageSizefromOriginalSize:[representation dimensions]]; - displayedImage = [self cropImageFromRepresentation:representation withNewSize:imageSize]; - UIImage *image = [[UIImage alloc] initWithCGImage:displayedImage]; - [self setBubbleSize]; [_finalImage setImage:image]; + [_messageImageView setImage:thumb]; [_messageImageView setFullImageUrl:asset]; [_messageImageView stopLoading]; _messageImageView.hidden = YES; _imageGestureRecognizer.enabled = YES; - _cancelButton.hidden = _fileTransferProgress.hidden = _downloadButton.hidden = YES; + //_cancelButton.hidden = _fileTransferProgress.hidden = _downloadButton.hidden = YES; _finalImage.hidden = NO; - ChatConversationTableView *tableView = VIEW(ChatConversationView).tableController; - [tableView.photoSizesDict setObject:@(bubbleSize.height) forKey:self.indexInTable]; - [tableView.tableView beginUpdates]; - [tableView.tableView endUpdates]; + LOGD(@"ZBOUB"); }); } @@ -128,7 +120,6 @@ return; } [super update]; - const char *url = linphone_chat_message_get_external_body_url(self.message); BOOL is_external = (url && (strstr(url, "http") == url)) || linphone_chat_message_get_file_transfer_information(self.message); @@ -202,8 +193,7 @@ if (!fullScreenImage) { newFrame.size.height -= _imageSubView.frame.size.height; } - //newFrame.size.height *= 10; - //_messageImageView.frame = newFrame; + _messageImageView.frame = newFrame; } - (IBAction)onDownloadClick:(id)event { @@ -326,21 +316,16 @@ return imgRef; } - -static const CGFloat CELL_MIN_HEIGHT = 60.0f; -static const CGFloat CELL_MIN_WIDTH = 190.0f; -static const CGFloat CELL_IMAGE_HEIGHT = 100.0f; -static const CGFloat CELL_IMAGE_WIDTH = 100.0f; -static const CGFloat CELL_MESSAGE_Y_MARGIN = 0; // 44; - - (void)layoutSubviews { [super layoutSubviews]; - if (!isAssetLoaded) return; + //if (!isAssetLoaded) return; UITableView *tableView = VIEW(ChatConversationView).tableController.tableView; BOOL is_outgoing = linphone_chat_message_is_outgoing(super.message); CGRect bubbleFrame = super.bubbleView.frame; int origin_x; + bubbleSize = [UIChatBubbleTextCell ViewSizeForMessage:[self message] withWidth:chatTableView.tableView.frame.size.width]; + bubbleFrame.size = bubbleSize; if (tableView.isEditing) { @@ -354,46 +339,20 @@ static const CGFloat CELL_MESSAGE_Y_MARGIN = 0; // 44; super.bubbleView.frame = bubbleFrame; } -- (CGSize)getMediaMessageSizefromOriginalSize:(CGSize)originalSize{ - CGSize mediaSize = CGSizeMake(0, 0); - //CGRect screenRect = [[UIScreen mainScreen] bounds]; - if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) { - actualAvailableWidth = actualAvailableWidth *2/3; - } - int height = originalSize.height; - if (originalSize.width > actualAvailableWidth) { - height = originalSize.height * actualAvailableWidth / originalSize.width; - } - mediaSize.height = MIN(height, actualAvailableWidth); - mediaSize.width = MIN(actualAvailableWidth, originalSize.width); - return mediaSize; -} - -- (CGImageRef)cropImageFromRepresentation:(ALAssetRepresentation*)rep withNewSize:(CGSize)size { +- (CGImageRef)cropImageFromRepresentation:(ALAssetRepresentation*)rep { CGImageRef newImage = [rep fullResolutionImage]; CGSize originalSize = [rep dimensions]; // We resize in width and crop in height - if (originalSize.width > size.width) { - int height = originalSize.height * size.width / originalSize.width; - newImage = [self.class resizeCGImage:newImage toWidth:size.width andHeight:height]; + if (originalSize.width > imageSize.width) { + int height = originalSize.height * imageSize.width / originalSize.width; + newImage = [self.class resizeCGImage:newImage toWidth:imageSize.width andHeight:height]; originalSize.height = height; } - CGRect cropRect = CGRectMake(0, 0, size.width, size.height); - if (size.height < originalSize.height) cropRect.origin.y = (originalSize.height - size.height)/2; + CGRect cropRect = CGRectMake(0, 0, imageSize.width, imageSize.height); + if (imageSize.height < originalSize.height) cropRect.origin.y = (originalSize.height - imageSize.height)/2; newImage = CGImageCreateWithImageInRect(newImage, cropRect); return newImage; } -- (void) setBubbleSize { - if (!isAssetLoaded) { - NSString *localImage = [LinphoneManager getMessageAppDataForKey:@"localimage" inMessage:self.message]; - bubbleSize = (localImage != nil) ? CGSizeMake(CELL_IMAGE_WIDTH, CELL_IMAGE_HEIGHT) : CGSizeMake(50, 50); - bubbleSize.height += self.avatarImage.frame.size.height + self.imdmLabel.frame.size.height + CELL_MESSAGE_Y_MARGIN; - } - bubbleSize = CGSizeMake(imageSize.width, imageSize.height + self.avatarImage.frame.size.height + self.imdmLabel.frame.size.height + CELL_MESSAGE_Y_MARGIN); //We add the height of the avatar so the image is not distorted - bubbleSize.width = MAX(bubbleSize.width, CELL_MIN_WIDTH); - bubbleSize.height = MAX(bubbleSize.height, CELL_MIN_HEIGHT); -} - @end diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.h b/Classes/LinphoneUI/UIChatBubbleTextCell.h index 1650dc8b2..26966cbb5 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.h +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.h @@ -40,9 +40,8 @@ @property(weak, nonatomic) IBOutlet UIImageView *imdmIcon; @property(weak, nonatomic) IBOutlet UILabel *imdmLabel; -@property(nonatomic) NSIndexPath *indexInTable; - + (CGSize)ViewSizeForMessage:(LinphoneChatMessage *)chat withWidth:(int)width; ++ (CGSize)getMediaMessageSizefromOriginalSize:(CGSize)originalSize withWidth:(int)width; - (void)setEvent:(LinphoneEventLog *)event; - (void)setChatMessage:(LinphoneChatMessage *)message; diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.m b/Classes/LinphoneUI/UIChatBubbleTextCell.m index b5b254f1e..3f42a8ebc 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.m +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.m @@ -306,8 +306,6 @@ static const CGFloat CELL_MIN_HEIGHT = 60.0f; static const CGFloat CELL_MIN_WIDTH = 190.0f; static const CGFloat CELL_MESSAGE_X_MARGIN = 78 + 10.0f; static const CGFloat CELL_MESSAGE_Y_MARGIN = 52; // 44; -static const CGFloat CELL_IMAGE_HEIGHT = 100.0f; -static const CGFloat CELL_IMAGE_WIDTH = 100.0f; + (CGSize)ViewHeightForMessage:(LinphoneChatMessage *)chat withWidth:(int)width { NSString *messageText = [UIChatBubbleTextCell TextMessageForChat:chat]; @@ -328,7 +326,23 @@ static const CGFloat CELL_IMAGE_WIDTH = 100.0f; font:messageFont]; } else { NSString *localImage = [LinphoneManager getMessageAppDataForKey:@"localimage" inMessage:chat]; - size = (localImage != nil) ? CGSizeMake(CELL_IMAGE_WIDTH, CELL_IMAGE_HEIGHT) : CGSizeMake(50, 50); + NSURL *imageUrl = [NSURL URLWithString:localImage]; + __block CGSize originalImageSize = CGSizeMake(0, 0); + dispatch_semaphore_t sema = dispatch_semaphore_create(0); + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { + [LinphoneManager.instance.photoLibrary assetForURL:imageUrl + resultBlock:^(ALAsset *asset) { + originalImageSize = [[asset defaultRepresentation] dimensions]; + dispatch_semaphore_signal(sema); + } + failureBlock:^(NSError *error) { + LOGE(@"Can't read image"); + dispatch_semaphore_signal(sema); + }]; + }); + dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER); + + size = [self getMediaMessageSizefromOriginalSize:originalImageSize withWidth:width]; } size.width = MAX(size.width + CELL_MESSAGE_X_MARGIN, CELL_MIN_WIDTH); size.height = MAX(size.height + CELL_MESSAGE_Y_MARGIN, CELL_MIN_HEIGHT); @@ -375,4 +389,20 @@ static const CGFloat CELL_IMAGE_WIDTH = 100.0f; } } + ++ (CGSize)getMediaMessageSizefromOriginalSize:(CGSize)originalSize withWidth:(int)width { + CGSize mediaSize = CGSizeMake(0, 0); + int availableWidth = width - CELL_MESSAGE_X_MARGIN; + if (UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])) { + availableWidth = availableWidth /3; + } + int height = originalSize.height; + if (originalSize.width > availableWidth) { + height = originalSize.height * availableWidth / originalSize.width; + } + mediaSize.height = MIN(height, availableWidth); + mediaSize.width = MIN(availableWidth, originalSize.width); + return mediaSize; +} + @end