From 46fefb69883c38ae2fb5b25b382143be93f1e390 Mon Sep 17 00:00:00 2001 From: Danmei Chen Date: Tue, 9 Mar 2021 15:05:20 +0100 Subject: [PATCH] display as file if can not load image --- Classes/LinphoneUI/UIChatBubblePhotoCell.m | 30 ++++++++++++++-------- Classes/LinphoneUI/UIChatBubbleTextCell.m | 21 ++++++++++----- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/Classes/LinphoneUI/UIChatBubblePhotoCell.m b/Classes/LinphoneUI/UIChatBubblePhotoCell.m index de665dd05..119da2ab1 100644 --- a/Classes/LinphoneUI/UIChatBubblePhotoCell.m +++ b/Classes/LinphoneUI/UIChatBubblePhotoCell.m @@ -114,7 +114,9 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; }]; } -- (void) loadFileAsset { +- (void) loadFileAsset:(NSString *)name { + NSString *text = [NSString stringWithFormat:@"📎 %@",name]; + _fileName.text = text; dispatch_async(dispatch_get_main_queue(), ^{ _fileName.hidden = _fileView.hidden = _fileButton.hidden = NO; _imageGestureRecognizer.enabled = NO; @@ -170,14 +172,24 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; if (_messageImageView.image == nil) { NSData* data = [NSData dataWithContentsOfFile:filePath]; UIImage *image = [[UIImage alloc] initWithData:data]; - [self loadImageAsset:nil image:image]; - _imageGestureRecognizer.enabled = YES; + if (image) { + [self loadImageAsset:nil image:image]; + _imageGestureRecognizer.enabled = YES; + } else { + // compability with other platforms + [self loadFileAsset:fileName]; + } } } else if ([key isEqualToString:@"localvideo"]) { if (_messageImageView.image == nil) { UIImage* image = [UIChatBubbleTextCell getImageFromVideoUrl:[ChatConversationView getCacheFileUrl:fileName]]; - [self loadImageAsset:nil image:image]; - _imageGestureRecognizer.enabled = NO; + if (image) { + [self loadImageAsset:nil image:image]; + _imageGestureRecognizer.enabled = NO; + } else { + // compability with other platforms + [self loadFileAsset:fileName]; + } } } else if ([key isEqualToString:@"localfile"]) { if ([fileType isEqualToString:@"video"]) { @@ -190,9 +202,7 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; [self loadImageAsset:nil image:image]; _imageGestureRecognizer.enabled = YES; } else { - NSString *text = [NSString stringWithFormat:@"📎 %@",fileName]; - _fileName.text = text; - [self loadFileAsset]; + [self loadFileAsset:fileName]; } } @@ -279,9 +289,7 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; [self loadImageAsset:nil image:image]; _imageGestureRecognizer.enabled = YES; } else { - NSString *text = [NSString stringWithFormat:@"📎 %@",localFile]; - _fileName.text = text; - [self loadFileAsset]; + [self loadFileAsset:fileName]; } } } diff --git a/Classes/LinphoneUI/UIChatBubbleTextCell.m b/Classes/LinphoneUI/UIChatBubbleTextCell.m index bcb30af36..1c1db4154 100644 --- a/Classes/LinphoneUI/UIChatBubbleTextCell.m +++ b/Classes/LinphoneUI/UIChatBubbleTextCell.m @@ -363,6 +363,14 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; return [self ViewHeightForMessageText:chat withWidth:width textForImdn:nil]; } ++ (CGSize)ViewHeightForFile:(int)width { + CGSize fileSize = CGSizeMake(230, 50); + CGSize size = [self getMediaMessageSizefromOriginalSize:fileSize 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); + return size; +} + + (CGSize)ViewHeightForMessageText:(LinphoneChatMessage *)chat withWidth:(int)width textForImdn:(NSString *)imdnText{ NSString *messageText = [UIChatBubbleTextCell TextMessageForChat:chat]; static UIFont *messageFont = nil; @@ -423,12 +431,7 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; image = [[UIImage alloc] initWithData:data]; } } else { - // other files // todo for text - CGSize fileSize = CGSizeMake(230, 50); - size = [self getMediaMessageSizefromOriginalSize:fileSize 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); - return size; + return [self ViewHeightForFile:width]; } originalImageSize = image.size; @@ -441,9 +444,15 @@ static const CGFloat CELL_IMAGE_X_MARGIN = 100; if (localImage && [[NSFileManager defaultManager] fileExistsAtPath:[[LinphoneManager cacheDirectory] stringByAppendingPathComponent:localImage]]) { NSData* data = [ChatConversationView getCacheFileData:localImage]; UIImage *image = [[UIImage alloc] initWithData:data]; + if (!image) { + return [self ViewHeightForFile:width]; + } originalImageSize = image.size; } else if (localVideo && [[NSFileManager defaultManager] fileExistsAtPath:[[LinphoneManager cacheDirectory] stringByAppendingPathComponent:localVideo]]) { UIImage *image = [UIChatBubbleTextCell getImageFromVideoUrl:[ChatConversationView getCacheFileUrl:localVideo]]; + if (!image) { + return [self ViewHeightForFile:width]; + } originalImageSize = image.size; } else { // support previous versions