Skip to content

Commit 2e58398

Browse files
fix: prevent displaying file icon when image uri is wrong
1 parent 9a00b4d commit 2e58398

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

ios/styles/ImageStyle.mm

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,15 @@ - (ImageData *)getImageDataAt:(NSUInteger)location
105105

106106
- (void)addImageAtRange:(NSRange)range imageData:(ImageData *)imageData
107107
{
108+
UIImage *img = [self prepareImageFromUri:imageData.uri];
109+
110+
if (img == nil) {
111+
// Could not create image from URI, stop here to avoid the "OBJ" icon
112+
return;
113+
}
114+
108115
NSMutableDictionary *attributes = [@{
109-
NSAttachmentAttributeName: [self prepareImageAttachement:imageData.uri width:imageData.width height:imageData.height],
116+
NSAttachmentAttributeName: [self prepareImageAttachement:img width:imageData.width height:imageData.height],
110117
ImageAttributeName: imageData,
111118
} mutableCopy];
112119

@@ -134,11 +141,8 @@ - (void)addImage:(NSString *)uri width:(CGFloat)width height:(CGFloat)height {
134141
[self addImageAtRange:_input->textView.selectedRange imageData:data];
135142
}
136143

137-
-(NSTextAttachment *)prepareImageAttachement:(NSString *)uri width:(CGFloat)width height:(CGFloat)height
144+
-(NSTextAttachment *)prepareImageAttachement:(UIImage *)image width:(CGFloat)width height:(CGFloat)height
138145
{
139-
NSURL *url = [NSURL URLWithString:uri];
140-
NSData *imgData = [NSData dataWithContentsOfURL:url];
141-
UIImage *image = [UIImage imageWithData:imgData];
142146

143147
NSTextAttachment *attachment = [[NSTextAttachment alloc] init];
144148
attachment.image = image;
@@ -147,4 +151,13 @@ -(NSTextAttachment *)prepareImageAttachement:(NSString *)uri width:(CGFloat)widt
147151
return attachment;
148152
}
149153

154+
- (UIImage *)prepareImageFromUri:(NSString *)uri
155+
{
156+
NSURL *url = [NSURL URLWithString:uri];
157+
NSData *imgData = [NSData dataWithContentsOfURL:url];
158+
UIImage *image = [UIImage imageWithData:imgData];
159+
160+
return image;
161+
}
162+
150163
@end

0 commit comments

Comments
 (0)